Several methods of mysqli processing query result set in PHP

Source: Internet
Author: User

<?PHP$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   (fetches a row of results in an associative array, a numeric index array, or both)

 <? php          $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/> ";  // fetch_array method returns when no more results are returned null// The return result can be an associative array or a numeric array index, so $row["id", $row [1] can be  

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

<?php
        $sql = "SELECT * from user";        $result = $link->query ($sql);        while ($row = $result->fetch_object ()) {            echo "ID:". $row->id. " User name: ". $row->name." Password: ". $row->password." <br/> ";        } If no more rows are returned, the result returned by null//is the object that is to be called as an object

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

<?php
        $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 in an associative array)

<?php
        $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. )

<?php
        $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 ( returns column information in the result set as an object )

<?php
        $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 )

<?php
        $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 () 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

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.