Home >Backend Development >PHP Tutorial >4 ways to obtain records in the result set
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 '
User ID | Name | Department number | Contact address | Contact number | '.$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 '
|
---|