MysqliDb::lock PHP Method

lock() public method

Locks a table for R/W action.
Author: Jonas Barascu
public lock ( string $table ) : MysqliDb
$table string The table to be locked. Can be a table or a view.
return MysqliDb if succeeeded;
    public function lock($table)
    {
        // Main Query
        $this->_query = "LOCK TABLES";
        // Is the table an array?
        if (gettype($table) == "array") {
            // Loop trough it and attach it to the query
            foreach ($table as $key => $value) {
                if (gettype($value) == "string") {
                    if ($key > 0) {
                        $this->_query .= ",";
                    }
                    $this->_query .= " " . self::$prefix . $value . " " . $this->_tableLockMethod;
                }
            }
        } else {
            // Build the table prefix
            $table = self::$prefix . $table;
            // Build the query
            $this->_query = "LOCK TABLES " . $table . " " . $this->_tableLockMethod;
        }
        // Exceute the query unprepared because LOCK only works with unprepared statements.
        $result = $this->queryUnprepared($this->_query);
        $errno = $this->mysqli()->errno;
        // Reset the query
        $this->reset();
        // Are there rows modified?
        if ($result) {
            // Return true
            // We can't return ourself because if one table gets locked, all other ones get unlocked!
            return true;
        } else {
            throw new Exception("Locking of table " . $table . " failed", $errno);
        }
        // Return the success value
        return false;
    }