Home >Backend Development >PHP Tutorial >4 ways to obtain records in the result set

4 ways to obtain records in the result set

WBOY
WBOYOriginal
2016-08-08 09:26:121761browse

First use SQL to create a table, and insert the data in the table
Create Table Contractinfo (
UID Mediumint (8)
UNSIGNED NOT NULL Auto_increment,#contact ID
name varchar (50) not null,#name


MENTID Char (3 ) NOT NULL, Contact person’s email
PRIMARY KEY(uid) # Set the user ID (i.e. uid) as the primary key
);
        The four functions fetch_row(), fetch_array(), fetch_assoc(), fetch_object() use similar methods to read the result data rows in sequence. They only differ in the way they reference fields
What they have in common: Each call will automatically return the next result record. If the end of the result data table has been reached, false will be returned
1. $result->fetch_row()
Obtaining a result record from the result set and storing the value in an index array is the most convenient of the four methods.
Each field needs to be read in the form of $row[$n], where $row is an array returned from a row of records obtained from the result set, and $n is a continuous integer subscript.
Because it returns an index array, it can also be used in conjunction 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'); Contact for D01 All the person's name and email are taken out and stored in the result set*/

  $result=$mysqli->query("SELECT name,email FROM contactInfo WHERE departmentId='D01'");
                                                                             .                                      Person's name and email: ';
echo '

    ';
    while(list($name,$email)=$result->fetch_row()){
    echo '
  1. '.$name :$email.'
  2. ';
    }
    echo '
';
?>
2. $result->fetch_assoc()
This method will return an associative array For a result record, the field name of the data represents the key, and the field content represents 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 ($ rw = $ result-& gt; fetch_assoc ()) {
// Note $ result-& gt; fetch_accoc () is to return the associated array, even if you use $ key_name (key value)

echo '& lt; TR. align = "cneter">';
echo '';
';
                                                                                                                                                               echo '';
  echo '';
  echo '

Contact information table

The tag provides a brief description of the table, possibly to make it easier for search engines to find

echo '
User IDName Department numberContact addressContact numberE-mail '.$row['uid'].' '.$row['departmentId'].' '.$row['phone'].''.$row['email'].' & lt;/td & gt; ';
echo' & lt;/trb; '; close ();
?>
3. $result->fetch_array()
This method can be said to be a combined version of the fetch()_row and fetch_assco() methods. It can obtain each record of the result set as an associative array or A numerically indexed array, or obtained as both an associative array and an indexed array. By default, both arrays are obtained at the same time. This default behavior can be modified by passing different values ​​to this method as follows.
MYSQLI_ASSOC : The record is returned as an associative array, the field name is the key, and the field content is the value.
MYSQLI_NUM : Records are returned as an index array, sorted in the order of the field names specified in the query.
MYSQLI_BOTH : This is the default value, records are returned as both an associative array and an index array.
4. $result->fetch_object()
This method is different from the previous three methods. It will return a result record in the form of an object instead of an array. Each of its fields needs to be accessed in the form of objects, and 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 '';
                                                                                                                                                                                            ';
                                                                                                                                              ,,, echo ''; echo '';
' echo '';
} Method, each call will automatically return the next result record. If you want to change the order of 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 attribute in the result set object to give the number of records in the result data 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 above four methods.

The above introduces the four methods of obtaining records in the result set, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Contact information table

User IDNameDepartment numberContact address Contact numberE-mail'.$rowObj->uid.'''.$rowObj->phone.''.$rowObj->email.'