📄 mysqli.php
字号:
* @return int The number of rows returned from the most recent query. */ function getNumRows( $cur=null ) { return mysqli_num_rows( $cur ? $cur : $this->_cursor ); } /** * This method loads the first field of the first row returned by the query. * * @access public * @return The value returned in the query or null if the query failed. */ function loadResult() { if (!($cur = $this->query())) { return null; } $ret = null; if ($row = mysqli_fetch_row( $cur )) { $ret = $row[0]; } mysqli_free_result( $cur ); return $ret; } /** * Load an array of single field results into an array * * @access public */ function loadResultArray($numinarray = 0) { if (!($cur = $this->query())) { return null; } $array = array(); while ($row = mysqli_fetch_row( $cur )) { $array[] = $row[$numinarray]; } mysqli_free_result( $cur ); return $array; } /** * Fetch a result row as an associative array * * @access public * @return array */ function loadAssoc() { if (!($cur = $this->query())) { return null; } $ret = null; if ($array = mysqli_fetch_assoc( $cur )) { $ret = $array; } mysqli_free_result( $cur ); return $ret; } /** * Load a assoc list of database rows * * @access public * @param string The field name of a primary key * @return array If <var>key</var> is empty as sequential list of returned records. */ function loadAssocList( $key='' ) { if (!($cur = $this->query())) { return null; } $array = array(); while ($row = mysqli_fetch_assoc( $cur )) { if ($key) { $array[$row[$key]] = $row; } else { $array[] = $row; } } mysqli_free_result( $cur ); return $array; } /** * This global function loads the first row of a query into an object * * @access public * @return object */ function loadObject( ) { if (!($cur = $this->query())) { return null; } $ret = null; if ($object = mysqli_fetch_object( $cur )) { $ret = $object; } mysqli_free_result( $cur ); return $ret; } /** * Load a list of database objects * * If <var>key</var> is not empty then the returned array is indexed by the value * the database key. Returns <var>null</var> if the query fails. * * @access public * @param string The field name of a primary key * @return array If <var>key</var> is empty as sequential list of returned records. */ function loadObjectList( $key='' ) { if (!($cur = $this->query())) { return null; } $array = array(); while ($row = mysqli_fetch_object( $cur )) { if ($key) { $array[$row->$key] = $row; } else { $array[] = $row; } } mysqli_free_result( $cur ); return $array; } /** * Description * * @access public * @return The first row of the query. */ function loadRow() { if (!($cur = $this->query())) { return null; } $ret = null; if ($row = mysqli_fetch_row( $cur )) { $ret = $row; } mysqli_free_result( $cur ); return $ret; } /** * Load a list of database rows (numeric column indexing) * * If <var>key</var> is not empty then the returned array is indexed by the value * the database key. Returns <var>null</var> if the query fails. * * @access public * @param string The field name of a primary key * @return array If <var>key</var> is empty as sequential list of returned records. */ function loadRowList( $key=null ) { if (!($cur = $this->query())) { return null; } $array = array(); while ($row = mysqli_fetch_row( $cur )) { if ($key !== null) { $array[$row[$key]] = $row; } else { $array[] = $row; } } mysqli_free_result( $cur ); return $array; } /** * Inserts a row into a table based on an objects properties * * @access public * @param string The name of the table * @param object An object whose properties match table fields * @param string The name of the primary key. If provided the object property is updated. */ function insertObject( $table, &$object, $keyName = NULL ) { $fmtsql = 'INSERT INTO '.$this->nameQuote($table).' ( %s ) VALUES ( %s ) '; $fields = array(); foreach (get_object_vars( $object ) as $k => $v) { if (is_array($v) or is_object($v) or $v === NULL) { continue; } if ($k[0] == '_') { // internal field continue; } $fields[] = $this->nameQuote( $k ); $values[] = $this->isQuoted( $k ) ? $this->Quote( $v ) : (int) $v; } $this->setQuery( sprintf( $fmtsql, implode( ",", $fields ) , implode( ",", $values ) ) ); if (!$this->query()) { return false; } $id = $this->insertid(); if ($keyName && $id) { $object->$keyName = $id; } return true; } /** * Description * * @access public * @param [type] $updateNulls */ function updateObject( $table, &$object, $keyName, $updateNulls=true ) { $fmtsql = 'UPDATE '.$this->nameQuote($table).' SET %s WHERE %s'; $tmp = array(); foreach (get_object_vars( $object ) as $k => $v) { if( is_array($v) or is_object($v) or $k[0] == '_' ) { // internal or NA field continue; } if( $k == $keyName ) { // PK not to be updated $where = $keyName . '=' . $this->Quote( $v ); continue; } if ($v === null) { if ($updateNulls) { $val = 'NULL'; } else { continue; } } else { $val = $this->isQuoted( $k ) ? $this->Quote( $v ) : (int) $v; } $tmp[] = $this->nameQuote( $k ) . '=' . $val; } $this->setQuery( sprintf( $fmtsql, implode( ",", $tmp ) , $where ) ); return $this->query(); } /** * Description * * @access public */ function insertid() { return mysqli_insert_id( $this->_resource ); } /** * Description * * @access public */ function getVersion() { return mysqli_get_server_info( $this->_resource ); } /** * Assumes database collation in use by sampling one text field in one table * * @access public * @return string Collation in use */ function getCollation () { if ( $this->hasUTF() ) { $this->setQuery( 'SHOW FULL COLUMNS FROM #__content' ); $array = $this->loadAssocList(); return $array['4']['Collation']; } else { return "N/A (mySQL < 4.1.2)"; } } /** * Description * * @access public * @return array A list of all the tables in the database */ function getTableList() { $this->setQuery( 'SHOW TABLES' ); return $this->loadResultArray(); } /** * Shows the CREATE TABLE statement that creates the given tables * * @access public * @param array|string A table name or a list of table names * @return array A list the create SQL for the tables */ function getTableCreate( $tables ) { settype($tables, 'array'); //force to array $result = array(); foreach ($tables as $tblval) { $this->setQuery( 'SHOW CREATE table ' . $this->getEscaped( $tblval ) ); $rows = $this->loadRowList(); foreach ($rows as $row) { $result[$tblval] = $row[1]; } } return $result; } /** * Retrieves information about the given tables * * @access public * @param array|string A table name or a list of table names * @param boolean Only return field types, default true * @return array An array of fields by table */ function getTableFields( $tables, $typeonly = true ) { settype($tables, 'array'); //force to array $result = array(); foreach ($tables as $tblval) { $this->setQuery( 'SHOW FIELDS FROM ' . $tblval ); $fields = $this->loadObjectList(); if($typeonly) { foreach ($fields as $field) { $result[$tblval][$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type ); } } else { foreach ($fields as $field) { $result[$tblval][$field->Field] = $field; } } } return $result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -