English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_fetch_all() function retrieves all rows from the result set as an associative array, or a numeric array, or both.
PHP result object (mysqli_result class) represents the MySQL result returned by SELECT or DESCRIBE or EXPLAIN queries.
The function mysqli_fetch_all serves to: accept a result object as a parameter, and retrieve all rows in the given result object.
mysqli_fetch_all($result, [$type]);
Serial Number | Parameters and Description |
---|---|
1 | result (required) This is an identifier representing the result object. |
2 | type (required) This is an integer value that specifies the type of the returned array. The value of type is one of the following-
|
The PHP mysqli_fetch_all() function returns an array (associative or numeric), which contains the rows of the result object.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_fetch_all()Usage of the function (procedural style), obtaining all 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"); //Obtain all rows of the result $rows = mysqli_fetch_all($res); print_r($rows); //End statement mysqli_free_result($res); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record..... Array ( [0] => Array ( [0] => 1 [1] => Sikhar [2] => Dhawan [3] => Delhi [4] => India ) [1] => Array ( [0] => 2 [1] => Jonathan [2] => Trott [3] => Cape Town [4] => South Africa ) [2] => Array ( [0] => 3 [1] => Kumara [2] => Sangakkara [3] => Matale [4] => Srilanka ) )
In the object-oriented style, the syntax of this function is$result->fetch_all();.The following is an example of this function in an object-oriented style, retrieving all rows from the result set as an associative array:
<?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(); //Retrieve result $result = $stmt->get_result(); //Read all rows $rows = $result->fetch_all(); print_r($rows); //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Array ( [0] => Array ( [0] => Raju [1] => 25 ) [1] => Array ( [0] => Rahman [1] => 30 ) )