English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_use_result() function initializes the retrieval of the result set from the query executed last time using mysqli_real_query().
mysqli_use_result()The function starts to retrieve the result set from the previous executed query.
mysqli_use_result($con)
Serial number | Parameters and descriptions |
---|---|
1 | con(required) This is an object representing the connection to the MySQL Server. |
mysqli_use_result()The function returns the result object and the boolean value false in case of an error.
This function was originally in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_use_result()Function usage (procedural style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "test"); //Execute multiple queries, separated by semicolons. $query = "SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials"; $res = mysqli_multi_query($con, $query); $count = 0; if ($res) { do { $count = $count+1; mysqli_use_result($con); while (mysqli_next_result($con)); } print("Number of result sets: ".$count); mysqli_close($con); ?>
Output Result
Number of result sets: 3
In object-oriented style, the syntax of this function is }}$con->use_result();.Here is an example of this function in object-oriented style;
<?php $con = new mysqli("localhost", "root", "password", "test"); //Multiple queries $res = $con->multi_query("SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials"); $count = 0; if ($res) { do { $count = $count+1; $con-> use_result(); } while ($con->next_result()); } print("Number of result sets: ".$count); //Close connection $res = $con -> close(); ?>
Output Result
Number of result sets: 3
The following example retrieves all result sets of multiple queries-
//Establish connection $con = mysqli_connect("localhost", "root", "password", "test"); //Execute multiple queries $query = "SELECT * FROM players;SELECT * FROM emp"; $res = mysqli_multi_query($con, $query); if ($res) { do { if ($result = mysqli_use_result($con)) { while ($row = mysqli_fetch_row($result)) { print("Name: ".$row[0]."\n"); print("Age: ".$row[1]."\n"); } mysqli_free_result($result); } if (mysqli_more_results($con)) { print("\n"); } } while (mysqli_use_result($con)); } mysqli_close($con);
Output Result
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25 Name: Raju Age: 25 Name: Rahman Age: 30 Name: Ramani Age: 22