4 ways to get records in result sets

Source: Internet
Author: User

Create a table with SQL first and insert data into the table

CREATE TABLE ContactInfo (

UID Mediumint (8)
Unsigned not NULL auto_increment, #联系人ID

Name varchar (not NULL), #姓名

Deparmentid char (3) not NULL, #部门编号

Address varchar (not NULL), #联系地址

Phone varchar, #联系电话

Email varchar (+), #联系人的电子邮件

PRIMARY key (UID) #设置用户ID (that is, UID) is the primary key
);







The four functions of Fetch_row (), Fetch_array (), Fetch_assoc (), Fetch_object () read the resulting data rows in a similar way. They differ only in how fields are referenced
What they have in common: each call will automatically return the next result record if it has reached the end of the result data table and returns false



1, $result->fetch_row ()
Getting a result record from the result set, storing the value in an indexed array, is the most convenient of the four methods.
Each field needs to be read in $row[$n], where $row is the array returned from a row of records fetched from the result set, $n a continuous integer subscript.
Because an indexed array is returned, it can also be used in conjunction with the list () function.


<?php

$mysqli =new mysqli ("localhost", "Mysql_user", "Mysql_pwd", "my_db_name");

if (Mysqli_connect_errno ()) {

printf ("Connection Failed:%s<br>", Mysqli_connect_error ());

Exit ();

}


$mysqli->query (' Set names gb2312 '); Set the charset to GB 2312 yards


/* Remove the contact name and email from the department number D01 to the result set */

$result = $mysqli->query ("Select Name,email from ContactInfo WHERE departmentid= ' D01 '");

Echo ' D01 department's contact name and email: ';

Echo ' <ol> ';

while (list ($name, $email) = $result->fetch_row ()) {

Echo ' <li> ' $name: $email. ' </li> ';

}

Echo ' </ol> ';

?>





2, $result->fetch_assoc ()
The method returns a result record in the form of an associative array, the field name of the data represents the key, and the field content represents the value.

<?php

$mysqli =new mysqli (' localhost ', ' mysql_user ', ' mysql_pwd ', ' my_db_name ');

if (Mysqli_connect_errno ()) {

printf ("Connection Failed:%s<br>", Mysqli_connect_error ());

Exit ();

}


$mysqli->query (' Set names gb2312 ');

$result =mysqli->query (' SELECT * from ContactInfo ');


Echo ' <table widht= "90%" border= "1" align= "center" > ";

Echo ' <caption><caption> tags provide a brief description of the table, which may be used to make search engines easier to find

Echo ' <th> user id</th><th> name </th><th> Department number </th><th> contact address </th><th > Contact Phone </th><th> email </th> ';



while ($row = $result->fetch_assoc ()) {
Note that $RESULT-&GT;FETCH_ACCOC () returns an associative array, that is, using $key_name (key value) to access the numeric value

Echo ' <tr align= ' Cneter > ';

Echo ' <td> ' $row [' uid ']. ' </td> ';

Echo ' <td> ' $row [' name ']. ' </td> ';

Echo ' <td> ' $row [' DepartmentID ']. ' </td> ';

Echo ' <td> ' $row [' address ']. ' </td> ';

Echo ' <td> ' $row [' phone ']. ' </td> ';

Echo ' <td> ' $row [' email ']. ' </td> ';

Echo ' </tr> ';

}


Echo ' </table> ';

$result->close ();

$mysqli->close ();

?>






3, $result->fetch_array ()
This method can be said to be a combined version of the fetch () _row and Fetch_assco () two methods, which can get each record of the result set as an associative array or a numeric index array, or both as an associative array and an indexed array. By default, both of these arrays are obtained. You can modify this default behavior by passing in the method with the following different values.

MYSQLI_ASSOC: The record is returned as an associative array, the field name is the key, and the field content is a value.

Mysqli_num: The record is returned as an indexed array, sorted by the field name specified in the query.

Mysqli_both: This is the default value, and the record is returned as an associative array and as an indexed array.






4, $result->fetch_object ()
This method differs from the previous three methods in that it returns a result record as an object, not an array. Each of its fields needs to be accessed as an object, and the names of the data columns distinguish the case of letters.

<?php

$mysqli =new mysqli ("localhost", "Mysql_user", "Mysql_pwd", "my_db_name");

if (Mysqli_connect_errno ()) {

printf ("Connection Failed:%s<br>", Mysqli_connect_error ());

Exit ();

}


$mysqli->query ("Set names gb2312");

$mysqli->query ("SELECT * from ContactInfo");


Echo ' <table width= "%90" border= "1" align= "center" > ";

Echo ' <caption>;
Echo ' <th> user id</th><th> name </th><th> Department number </th><th> contact address </th><th > Contact Phone </th><th> email </th> ';


while ($ROWOBJ = $result->fetch_object ()) {

Echo ' <tr align= ' center > ';

Echo ' <td> ' $rowObj->uid. ' </td> ';

Echo ' <td> ' $rowObj->name. ' </td> ';

Echo ' <td> ' $rowObj->deparmentid. ' </td> ';

Echo ' <td> ' $rowObj->address. ' </td> ';

Echo ' <td> ' $rowObj->phone. ' </td> ';

Echo ' <td> ' $rowObj->email. ' </td> ';

Echo ' </tr> ';
}

Echo ' </table> '
$result->close ();

$mysqli->close ();

?>





The above four result sets the method of traversing the data, each call will automatically return the next result record. If you want to change the order of this reading, you can use the Data_seek () method in the result set object to explicitly change the current record position. You can also use the Num_rows property in the result set object to give the number of records in the result data table. You can also use the Lengths property in the result object to return a group whose individual elements are the number of characters in each of the fields in the result record that were last read using the four methods above.

4 ways to get records in result sets

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.