Several methods of mysqli processing query result set in PHP, mysqli query results
Recently the PHP query MySQL processing result set of several methods are not quite clear of the place to consult the data, in this collation note
PHP uses the Mysqli_result class to handle the result set in several ways
Fetch_all () |
Fetches all the result rows and returns the result set in the form of associative data, numeric indexed arrays, or both. |
Fetch_array () |
Fetches a row of results in an associative array, a numeric indexed array, or both. |
Fetch_object () |
Returns the current row of the result set as an object. |
Fetch_row () |
Returns a row of results as an array of enumerations |
FETCH_ASSOC () |
Fetches a row of results as an associative array. |
Fetch_field_direct () |
Returns the metadata for a single field in the result set as an object. |
Fetch_field () |
returns the column information in the result set as an object . |
Fetch_fields () |
returns the column information representing the result set as an array of objects. |
Fetch_all (get all rows from the result set as associative arrays )
$sql= "SELECT * from user"; $result=$link->query ($sql); $row=$result->fetch_all (Mysqli_both); // parameters Mysql_assoc, Mysqli_num, Mysqli_both specify the type of array to produce $n=0; while ($n<mysqli_num_rows($result)) { echo "ID:". ) $row[$n] [id]. " User name: ". $row [$n] ["Name"]. " Password: ". $row [$n] ["Password"]. "
"; $n+ +; }
Fetch_array (Fetch a row of results in an associative array, a numeric index array, or both)
$sql= "SELECT * from user"; $result=$link->query ($sql); while ($row=$result, Fetch_array ()) { echo "ID:". $row["id"]. " User name: ". $row [1]. " Password: ". $row ["Password"]. "
"; } // The Fetch_array method returns a return null//if it no longer has a result, it can be an associative array or a numeric array index, so $row["id", $row [1] can
Fetch_object ( returns the current row of the result set as an object )
$sql ="select * from user"; $result=query ($sql) , $link while ($row = $result, Fetch_object ()) { "ID:". $row->id." User name: ". $row->name. " Password: ". $row->password. "
" ; } // returns NULL if no more rows are found // The returned result is an object that is to be called as an object
Fetch_row ( returns a row of results as an array of enumerations )
$sql= "SELECT * from user"; $result=$link->query ($sql); while ($row=$result, Fetch_row ()) { echo "ID:". $row[0]. " User name: ". $row [1]. " Password: ". $row [2]. "
"; } // No more rows are returned null//the array is called with a numeric subscript, a[0] is correct, a["id"] is not
Fetch_assoc ( fetch a row of results in an associative array )
$sql= "SELECT * from user"; $result=$link->query ($sql); while ($row=$result, Fetch_assoc ()) { echo "ID:". $row["id"]. " User name: ". $row ["Name"]. " Password: ". $row ["Password"]. "
"; } // returns null//with no more rows to access the array with the associated index, a["id"] correct, a[0] does not
Fetch_field_direct ( returns the meta-data for single fields in the result set as an object. )
$sql= "SELECT * from user"; $result=$link->query ($sql); $n=0; while (1) { if(! $row=$result->fetch_field_direct ($nbreak; Echo "Column name:". $row->name. " Table: ". $row->table. " Data type: ". $row->type. "
"; } // fetch_field_direct ($n) returns only a single column, so you have to call the method constantly, and return false when there is no column
Fetch_field ( returns column information in the result set as an object )
$sql= "SELECT * from user"; $result=$link->query ($sql); while ($row=$result, Fetch_field ()) { echo "column name:". $row->name. " Table: ". $row->table. " Data type: ". $row->type. "
"; } // The method retrieves all columns//returns the column information as an object//Returns an object property such as: Name-column name, table-the name of the column, type-the column, and so on
Fetch_fields ( Returns the column information representing the result set in an object array )
$sql= "SELECT * from user"; $result=$link->query ($sql); $row=$result-Fetch_fields (); foreach ($rowas$val) { echo "column name:". $val->name. " Table: ". $val->table. " Data type: ". $val->type. "
"; } // This method function is the same as the purpose Fetch_field//is not the same as the method returns an array of objects (such as: Echo $row [0]->name; output first column name), rather than retrieving one column at a time
Another: Mysqli_result class there are other ways
Field_tell () |
Returns the position of the field pointer |
Data_seek () |
Adjust the result pointer to an arbitrary row in the result set |
Num_fields () |
Returns the number of fields in the result set (number of columns) |
Field_seek () |
Adjust the field pointer to a specific field start position |
Free () |
Releasing memory associated with a result set |
Fetch_lengths () |
Returns the column length of the current row in the result set |
Num_rows () |
Returns the number of rows in the result set |
Reference: PHP Manual (http://php.net/manual/zh/class.mysqli-result.php)
http://www.bkjia.com/PHPjc/1059474.html www.bkjia.com true http://www.bkjia.com/PHPjc/1059474.html techarticle PHP mysqli Processing query result set of several methods, mysqli query results recently on the PHP query MySQL processing result set of several methods are not quite clear of the place to consult the data, in this collation note ...