In php, how does mysqli process the query result set? mysqli query result _ PHP Tutorial

Source: Internet
Author: User
In php, how does mysqli process the query result set. Several methods of mysqli processing the query result set in php, and some methods of querying the mysql processing result set in php are not quite clear recently, write down several methods for mysqli to process the query result set in php.

  I recently read the documents about the methods for querying mysql processing result sets in php.

Php uses the mysqli_result class to process result sets in the following ways:

Fetch_all () Crawls all the result rows and returns the result set by associating the data, value index array, or both.
Fetch_array () Crawls a row of results by means of an associated array, a numeric index array, or both.
Fetch_object () Returns the current row of the result set as an object.
Fetch_row () Returns a row of results in an enumerated array.
Fetch_assoc () Captures a row of results in an associated array.
Fetch_field_direct () Metadata of a single field in the returned result set as an object.
Fetch_field () Returns the column information in the result set as an object.
Fetch_fields () Returns the column information in the result set as an array of objects.

Fetch_all (retrieve all rows from the result set as an associated array)

$ SQL = "select * from user"; $ result = $ link-> query ($ SQL); $ row = $ result-> fetch_all (MYSQLI_BOTH ); // The MYSQL_ASSOC, MYSQLI_NUM, and MYSQLI_BOTH parameters define the generation of array types $ n = 0; while ($ n
 "; $ N ++ ;}

Fetch_array (Capture a row of results in an associated 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"]."
";} // If the fetch_array method does not have any results, NULL is returned. // The Returned result can be an associated array or a numerical array index, therefore, $ row ["id"] and $ row [1] can all be used.

Fetch_object(Returns the current row of the result set as an object.)

$ SQL = "select * from user"; $ result = $ link-> query ($ SQL); while ($ row = $ result-> fetch_object ()) {echo "ID :". $ row-> id. "user name :". $ row-> name. "Password :". $ row-> password."
";} // If no more rows exist, NULL is returned. // The Returned result is an object, which must be called as an object.

Fetch_row(Returns a row of results in an enumerated array.)

$ SQL = "select * from user"; $ result = $ link-> query ($ SQL); while ($ row = $ result-> fetch_row ()) {echo "ID :". $ row [0]. "user name :". $ row [1]. "Password :". $ row [2]."
";} // If no more rows exist, NULL is returned. // The array is called by numerical subscript. a [0] is correct, and a [" id "] is not

Fetch_assoc (capture a row of results in an associated 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"]."
";} // If no more rows exist, NULL is returned. // access the array with an associated index. if a [" id "] is correct and a [0] is incorrect, no

Fetch_field_direct(Metadata of a single field in the returned result set is a single column.)

$ SQL = "select * from user"; $ result = $ link-> query ($ SQL); $ n = 0; while (1) {if (! $ Row = $ result-> fetch_field_direct ($ n ++) break; 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 this method continuously. if this column is not present, false is returned.

Fetch_field(Returns the 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."
";}/// This method retrieves all columns // returns the column information as an object // returns the object attributes such as: name-column name, table-table name of the column, type-type of the column, and so on

Fetch_fields(Returns the column information in the result set as an array of objects.)

$ SQL = "select * from user"; $ result = $ link-> query ($ SQL); $ row = $ result-> fetch_fields (); foreach ($ row as $ val) {echo "column name :". $ val-> name. "Table :". $ val-> table. "data type :". $ val-> type."
";}// The function of this method is the same as that of the destination fetch_field // The method returns an array of objects (for example, echo $ row [0]-> name; output the name of the first column), instead of retrieving a column at a time.

In addition, the mysqli_result class has other methods.

Field_tell () Returns the position of the field pointer.
Data_seek () Adjust the result pointer to any row in the result set
Num_fields () Number of fields in the returned result set (number of columns)
Field_seek () Adjust the field pointer to the starting position of a specific field
Free () Releases memory related to a result set.
Fetch_lengths () Column length of the current row in the returned result set
Num_rows () Number of rows in the returned result set

Reference: php Manual (http://php.net/manual/zh/class.mysqli-result.php)


Methods used by the consumer to process the query result set. recently, the mysqli query results have been reviewed for some methods that are not clear about the mysql Query result set in php...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.