PHP mysqli_multi_query() Function
In this chapter you will learn:
- Definition for PHP mysqli_multi_query() Function
- Syntax for PHP mysqli_multi_query() Function
- Parameter for PHP mysqli_multi_query() Function
- Return for PHP mysqli_multi_query() Function
- Example - performs multiple queries against the database
- Example - Object oriented style
Definition
The mysqli_multi_query() function performs one or more queries separated with a semicolon against the database.
Syntax
Object oriented style
bool mysqli::multi_query ( string $query )
Procedural style
bool mysqli_multi_query ( mysqli $link , string $query )
Parameter
Parameter | Is Required | Description |
---|---|---|
connection | Required. | MySQL connection |
query | Required. | One or more queries seperated with semicolon |
Return
It returns FALSE if the query fails.
Example
The following code performs multiple queries against the database.
<?php/*from j av a 2 s . c om*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT Lastname FROM Persons;SELECT Country FROM Customers";
// Execute multi query
if (mysqli_multi_query($con,$sql)){
do{
// Store first result set
if ($result=mysqli_store_result($con)){
while ($row=mysqli_fetch_row($result)){
printf("%s\n",$row[0]);
}
mysqli_free_result($con);
}
}while (mysqli_next_result($con));
}
mysqli_close($con);
?>
Example 2
<?php// j a v a 2 s .c o m
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();SELECT Name FROM City";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
$mysqli->close();
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP mysqli_next_result() Function
- Syntax for PHP mysqli_next_result() Function
- Parameter for PHP mysqli_next_result() Function
- Return for PHP mysqli_next_result() Function
- Example - prepare the next result set
Home » PHP Tutorial » PHP MySQLi Functions