📄 mysqli.php
字号:
// }}} // {{{ simpleQuery() /** * Sends a query to the database server * * @param string the SQL query string * * @return mixed + a PHP result resrouce for successful SELECT queries * + the DB_OK constant for other successful queries * + a DB_Error object on failure */ function simpleQuery($query) { $ismanip = DB::isManip($query); $this->last_query = $query; $query = $this->modifyQuery($query); if ($this->_db) { if (!@mysqli_select_db($this->connection, $this->_db)) { return $this->mysqliRaiseError(DB_ERROR_NODBSELECTED); } } if (!$this->autocommit && $ismanip) { if ($this->transaction_opcount == 0) { $result = @mysqli_query($this->connection, 'SET AUTOCOMMIT=0'); $result = @mysqli_query($this->connection, 'BEGIN'); if (!$result) { return $this->mysqliRaiseError(); } } $this->transaction_opcount++; } $result = @mysqli_query($this->connection, $query); if (!$result) { return $this->mysqliRaiseError(); } if (is_object($result)) { return $result; } return DB_OK; } // }}} // {{{ nextResult() /** * Move the internal mysql result pointer to the next available result. * * This method has not been implemented yet. * * @param resource $result a valid sql result resource * @return false * @access public */ function nextResult($result) { return false; } // }}} // {{{ fetchInto() /** * Places a row from the result set into the given array * * Formating of the array and the data therein are configurable. * See DB_result::fetchInto() for more information. * * This method is not meant to be called directly. Use * DB_result::fetchInto() instead. It can't be declared "protected" * because DB_result is a separate object. * * @param resource $result the query result resource * @param array $arr the referenced array to put the data in * @param int $fetchmode how the resulting array should be indexed * @param int $rownum the row number to fetch (0 = first row) * * @return mixed DB_OK on success, NULL when the end of a result set is * reached or on failure * * @see DB_result::fetchInto() */ function fetchInto($result, &$arr, $fetchmode, $rownum = null) { if ($rownum !== null) { if (!@mysqli_data_seek($result, $rownum)) { return null; } } if ($fetchmode & DB_FETCHMODE_ASSOC) { $arr = @mysqli_fetch_array($result, MYSQLI_ASSOC); if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) { $arr = array_change_key_case($arr, CASE_LOWER); } } else { $arr = @mysqli_fetch_row($result); } if (!$arr) { return null; } if ($this->options['portability'] & DB_PORTABILITY_RTRIM) { /* * Even though this DBMS already trims output, we do this because * a field might have intentional whitespace at the end that * gets removed by DB_PORTABILITY_RTRIM under another driver. */ $this->_rtrimArrayValues($arr); } if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) { $this->_convertNullArrayValuesToEmpty($arr); } return DB_OK; } // }}} // {{{ freeResult() /** * Deletes the result set and frees the memory occupied by the result set * * This method is not meant to be called directly. Use * DB_result::free() instead. It can't be declared "protected" * because DB_result is a separate object. * * @param resource $result PHP's query result resource * * @return bool TRUE on success, FALSE if $result is invalid * * @see DB_result::free() */ function freeResult($result) { return @mysqli_free_result($result); } // }}} // {{{ numCols() /** * Gets the number of columns in a result set * * This method is not meant to be called directly. Use * DB_result::numCols() instead. It can't be declared "protected" * because DB_result is a separate object. * * @param resource $result PHP's query result resource * * @return int the number of columns. A DB_Error object on failure. * * @see DB_result::numCols() */ function numCols($result) { $cols = @mysqli_num_fields($result); if (!$cols) { return $this->mysqliRaiseError(); } return $cols; } // }}} // {{{ numRows() /** * Gets the number of rows in a result set * * This method is not meant to be called directly. Use * DB_result::numRows() instead. It can't be declared "protected" * because DB_result is a separate object. * * @param resource $result PHP's query result resource * * @return int the number of rows. A DB_Error object on failure. * * @see DB_result::numRows() */ function numRows($result) { $rows = @mysqli_num_rows($result); if ($rows === null) { return $this->mysqliRaiseError(); } return $rows; } // }}} // {{{ autoCommit() /** * Enables or disables automatic commits * * @param bool $onoff true turns it on, false turns it off * * @return int DB_OK on success. A DB_Error object if the driver * doesn't support auto-committing transactions. */ function autoCommit($onoff = false) { // XXX if $this->transaction_opcount > 0, we should probably // issue a warning here. $this->autocommit = $onoff ? true : false; return DB_OK; } // }}} // {{{ commit() /** * Commits the current transaction * * @return int DB_OK on success. A DB_Error object on failure. */ function commit() { if ($this->transaction_opcount > 0) { if ($this->_db) { if (!@mysqli_select_db($this->connection, $this->_db)) { return $this->mysqliRaiseError(DB_ERROR_NODBSELECTED); } } $result = @mysqli_query($this->connection, 'COMMIT'); $result = @mysqli_query($this->connection, 'SET AUTOCOMMIT=1'); $this->transaction_opcount = 0; if (!$result) { return $this->mysqliRaiseError(); } } return DB_OK; } // }}} // {{{ rollback() /** * Reverts the current transaction * * @return int DB_OK on success. A DB_Error object on failure. */ function rollback() { if ($this->transaction_opcount > 0) { if ($this->_db) { if (!@mysqli_select_db($this->connection, $this->_db)) { return $this->mysqliRaiseError(DB_ERROR_NODBSELECTED); } } $result = @mysqli_query($this->connection, 'ROLLBACK'); $result = @mysqli_query($this->connection, 'SET AUTOCOMMIT=1'); $this->transaction_opcount = 0; if (!$result) { return $this->mysqliRaiseError(); } } return DB_OK; } // }}} // {{{ affectedRows() /** * Determines the number of rows affected by a data maniuplation query * * 0 is returned for queries that don't manipulate data. * * @return int the number of rows. A DB_Error object on failure. */ function affectedRows() { if (DB::isManip($this->last_query)) { return @mysqli_affected_rows($this->connection); } else { return 0; } } // }}} // {{{ nextId() /** * Returns the next free id in a sequence * * @param string $seq_name name of the sequence * @param boolean $ondemand when true, the seqence is automatically * created if it does not exist * * @return int the next id number in the sequence. * A DB_Error object on failure. * * @see DB_common::nextID(), DB_common::getSequenceName(), * DB_mysqli::createSequence(), DB_mysqli::dropSequence() */ function nextId($seq_name, $ondemand = true) { $seqname = $this->getSequenceName($seq_name); do { $repeat = 0; $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = $this->query('UPDATE ' . $seqname . ' SET id = LAST_INSERT_ID(id + 1)'); $this->popErrorHandling(); if ($result === DB_OK) { // COMMON CASE $id = @mysqli_insert_id($this->connection); if ($id != 0) { return $id; } // EMPTY SEQ TABLE // Sequence table must be empty for some reason, // so fill it and return 1 // Obtain a user-level lock $result = $this->getOne('SELECT GET_LOCK(' . "'${seqname}_lock', 10)"); if (DB::isError($result)) { return $this->raiseError($result); } if ($result == 0) { return $this->mysqliRaiseError(DB_ERROR_NOT_LOCKED); } // add the default value $result = $this->query('REPLACE INTO ' . $seqname . ' (id) VALUES (0)'); if (DB::isError($result)) { return $this->raiseError($result); } // Release the lock $result = $this->getOne('SELECT RELEASE_LOCK(' . "'${seqname}_lock')"); if (DB::isError($result)) { return $this->raiseError($result); } // We know what the result will be, so no need to try again return 1; } elseif ($ondemand && DB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { // ONDEMAND TABLE CREATION $result = $this->createSequence($seq_name); // Since createSequence initializes the ID to be 1, // we do not need to retrieve the ID again (or we will get 2) if (DB::isError($result)) { return $this->raiseError($result); } else { // First ID of a newly created sequence is 1 return 1; } } elseif (DB::isError($result) && $result->getCode() == DB_ERROR_ALREADY_EXISTS) { // BACKWARDS COMPAT // see _BCsequence() comment $result = $this->_BCsequence($seqname); if (DB::isError($result)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -