Four methods for getting records in the result set

Source: Internet
Author: User
: This article mainly introduces four methods for getting records in the result set. For more information about PHP tutorials, see. Create a table using SQL and insert data into the table
Create table contactInfo (
Uid mediumint (8)
Unsigned not null AUTO_INCREMENT, # Contact ID
Name varchar (50) not null, # name

DeparmentId char (3) not null, # Department ID
Address varchar (80) not null, # contact address
Phone varchar (20), # contact number

Email varchar (100), # contact email
Primary key (uid) # set the user ID (uid) as the PRIMARY KEY
);
The fetch_row (), fetch_array (), fetch_assoc (), and fetch_object () functions read the result data rows in a similar way. They differ only in the way 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, false is returned.
1. $ result-> fetch_row ()
Obtain a result record from the result set and store the value in an index array, which is the most convenient of the four methods.
Each field needs to be read in the form of $ row [$ n], where $ row is the array returned by a row of records obtained from the result set, and $ n is a continuous integer subscript.
The returned index array can be used together with the list () function.
$ Mysqli = new mysqli ("localhost", "mysql_user", "mysql_pwd", "my_db_name ");
If (mysqli_connect_errno ()){
Printf ("connection failed: % s
", Mysqli_connect_error ());
Exit ();

}

$ Mysqli-> query ('set names gb2312 '); // set the character set to 2312 yards for the national standard

/* Retrieve all the contact names and emails numbered D01 from the department and save them to the result set */

$ Result = $ mysqli-> query ("SELECT name, email FROM contactInfo WHERE comment mentid = 'd01 '");

Echo 'd01 Department contact name and email :';
Echo'
    ';
    While (list ($ name, $ email) = $ result-> fetch_row ()){
    Echo'
  1. '. $ Name: $ email .'
  2. ';
    }
    Echo'
';
?>
2. $ result-> fetch_assoc ()
This method returns a result record in the form of an associated array. The field name of the data indicates the key and the field content indicates the value.
$ Mysqli = new mysqli ('localhost', 'MySQL _ user', 'MySQL _ pwd', 'My _ db_name ');
If (mysqli_connect_errno ()){
Printf ("connection failed: % s
", Mysqli_connect_error ());
Exit ();
}

$ Mysqli-> query ('set names gb2312 ');
$ Result = mysqli-> query ('select * FROM contactInfo ');

Echo'

















';Echo' '// '; While ($ row = $ result-> fetch_assoc ()){// Note that $ result-> fetch_accoc () is the returned joined array, that is, the access value using $ key_name (key value) Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';} Echo'
Contact info table Tags provide a brief description of the table, which may be used to make search engines easier to find

Echo'
User ID Name Department ID Contact Address Contact number Email
'. $ Row ['uid'].''. $ Row ['name'].''. $ Row ['Your mentid'].''. $ Row ['address'].''. $ Row ['phone'].''. $ Row ['email'].'
';
$ 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 () methods. each record in the result set can be obtained as an associated array or a numerical index array, alternatively, you can obtain the associated array and index array at the same time. By default, both arrays are obtained. You can pass in the following different values in this method to modify this default behavior.
MYSQLI_ASSOC: The record is returned as an associated array. The field name is the key and the field content is the value.
MYSQLI_NUM: records are returned as index arrays and sorted by the field names specified in the query.
MYSQLI_BOTH: this is the default value. records are returned as an associated array and an index array.
4. $ result-> fetch_object ()
Unlike the previous three methods, this method returns a result record in the form of an object instead of an array. Its fields must be accessed as objects. the names of data columns are case sensitive.
$ Mysqli = new mysqli ("localhost", "mysql_user", "mysql_pwd", "my_db_name ");
If (mysqli_connect_errno ()){
Printf ("connection failed: % s
", Mysqli_connect_error ());
Exit ();

}

$ Mysqli-> query ("set names gb2312 ");
$ Mysqli-> query ("SELECT * FROM contactInfo ");

Echo'














';Echo' ';Echo' '; While ($ rowObj = $ result-> fetch_object ()){Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';}Echo'
Contact info table
User ID Name Department ID Contact Address Contact number Email
'. $ RowObj-> uid .''. $ RowObj-> name .''. $ RowObj-> deparmentId .''. $ RowObj-> address .''. $ RowObj-> phone .''. $ RowObj-> email .'
'
$ Result-> close ();
$ Mysqli-> close ();
?>
The preceding four methods are used to traverse data. the next result record is automatically returned for each call. To change the reading order, you can use the data_seek () method in the result set object to explicitly change the location of the current record. You can also use the num_rows attribute in the result set object to display the number of records in the result table. You can also use the lengths attribute in the result object to return a group. each element of the array is the number of characters in each field in the result record that is finally read using the preceding four methods.

The above describes four methods for getting records in the result set, including the content, and hope to be helpful to friends who are interested in PHP tutorials.

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.