English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_fetch_assoc() function retrieves the result row from the result set as an associative array.
PHP result object (mysqli_result class) represents the MySQL result returned by SELECT or DESCRIBE or EXPLAIN query.
The function of mysqli_fetch_assoc is to: accept a result object as a parameter, retrieve the content of the current row in the given result object, and return it as an associative array or a numeric array.
mysqli_fetch_assoc($result);
Serial number | Parameters and descriptions |
---|---|
1 | result (required) This is the identifier representing the result object. |
The PHP mysqli_fetch_assoc() function returns an associative array containing the current row of the result object. If there are no more rows, this function will return NULL.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_fetch_assoc()Function usage (procedural style), retrieve rows from the result set as an associative array:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Create table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("Inserting record.....\n"); //Retrieve the content of the table $res = mysqli_query($con, "SELECT * FROM myplayers"); //Retrieve all rows as objects while($obj = mysqli_fetch_assoc($res)){ print("ID: ".$obj["ID"]."\n"); print("First_Name: ".$obj["First_Name"]."\n"); print("Last_Name: ".$obj["Last_Name"]."\n"); print("Place_Of_Birth: ".$obj["Place_Of_Birth"]."\n"); print("Country: ".$obj["Country"]."\n"); } //End statement mysqli_free_result($res); //Close connection mysqli_close($con); ?>
Output result
Create table..... Inserting record..... ID: 1 First_Name: Sikhar Last_Name: Dhawan Place_Of_Birth: Delhi Country: India ID: 2 First_Name: Jonathan Last_Name: Trott Place_Of_Birth: CapeTown Country: SouthAfrica ID: 3 First_Name: Kumara Last_Name: Sangakkara Place_Of_Birth: Matale Country: Srilanka
In the object-oriented style, the syntax of this function is$result->fetch_assoc();.Here is an example of this function in an object-oriented style;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), Age INT)"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Create table.....\n"); $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; //Execute statement $stmt->execute()); //Search results $result = $stmt->get_result()); //Get all rows as an array while($obj = $result->fetch_assoc()); print("Name: ".$obj["Name"]."\n"); print("Age: ".$obj["Age"]."\n"); } //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Name: Raju Age: 25 Name: Rahman Age: 30