Several methods of mysqli processing query result set in PHP

Source: Internet
Author: User

  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"]. " <br/> "; $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"]. " <br/> ";        } // 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= $linkquery ($sql);  while($row = $resultFetch_object ()) {echo"ID:". $row->id."User name:". $row->name."Password:". $row->password."<br/>"; }//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]. " <br/> ";        } // 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 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"]. " <br/> ";        } // 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 ($n++)) Break; Echo"Column name:".$row->name. " Table: ".$row->table. " Data type: ".$row->type. " <br/> "; }//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 ( )

        $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. " <br/> ";        } // 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($row  as $val){            Echo"Column name:".$val->name. " Table: ".$val->table. " Data type: ".$val->type. " <br/> "; }//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 ()   releases memory associated with a result set
fetch_lengths () Span style= "FONT-SIZE:13PX;" > 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)


Several methods of mysqli processing 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.