Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of mysqli processing result set of php query

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the php query mysqli processing result set analysis, hoping to supplement and update some knowledge, if you have other questions to understand, you can continue to follow my updated article in the industry information.

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

Fetch_all () grabs all the result rows and returns the result set as associated data, a numerically indexed array, or both. Fetch_array () grabs a row of results as an associative array, a numerically 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 enumerated array fetch_assoc () grabs a row of results as an associative array. Fetch_field_direct () returns metadata for a single field in the result set as an object. Fetch_field () returns column information in the result set as an object. Fetch_fields () returns column information that represents the result set as an array of objects.

Fetch_all (get all rows from the result set as an associative array)

$sql= "select * from user"; $result=$link- > query ($sql); $row=$result- > fetch_all (MYSQLI_BOTH); / / parameters MYSQL_ASSOC, MYSQLI_NUM, MYSQLI_BOTH specify the array type $nyst0; while ($nquery ($sql); while ($row=$result- > fetch_array ()) {echo "ID:". $row ["id"]. " User name: ". $row [1]." Password: ". $row [" password "].";} / / the fetch_array method returns NULL// when there is no more result, and the returned result can be either an associative array or a numeric array index, so $row ["id"] and $row [1] are fine.

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 there are no more rows, the result returned by NULL// is an object. Call it as an object.

Fetch_row (returns a row of results as an enumerated array)

$sql= "select * from user"; $result=$link- > query ($sql); while ($row=$result- > fetch_row ()) {echo "ID:". $row [0]. "username:". $row [1]. "password:". $row [2]. ";} / / return NULL// with numeric subscript to call array when there are no more lines, a [0] is correct, a [" id "] is not.

Fetch_assoc (crawls a row of results as 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"]. ";} / / return NULL// to access the array with the associated index when there are no more rows. A [" id "] is correct, but a [0] is not.

Fetch_field_direct (returns the metadata of a single field in the result set as an object and the information of a single column)

$sql= "select * from user"; $result=$link- > query ($sql); $sql; while (1) {if (! $row=$result- > fetch_field_direct ($nasty +)) 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 constantly and return false if there is no such 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.";} / / this method retrieves all columns / / returns column information as an object / / returns object properties such as: name-column name, table-table name of the column, type-type of the column, etc.

Fetch_fields (returns column information representing 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.";} / / this method has the same function as the destination fetch_field, except that the method returns an array of objects (for example: echo $row [0]-> the name of the first column of the name; output) instead of retrieving one column at a time.

Another: there are other methods for the mysqli_result class

Field_tell () returns the position of the field pointer data_seek () adjusts the result pointer to any row in the result set num_fields () returns the number of fields in the result set (number of columns) field_seek () adjusts the field pointer to a specific field start position free () frees 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

Read the above about php query mysqli processing result set analysis, hope to bring some help to everyone in practical application. Due to the limited space in this article, it is inevitable that there will be deficiencies and need to be supplemented. If you need more professional answers, you can contact us on the official website for 24-hour pre-sales and after-sales to help you answer questions at any time.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report