PHP mysqli multi_query() 函数
示例 - 面向对象的样式
执行多个针对数据库的查询:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";
// Execute multi query
if ($mysqli -> multi_query($sql)) {
do {
// Store first result set
if ($result = $mysqli -> store_result()) {
while ($row = $result -> fetch_row()) {
printf("%s\n", $row[0]);
}
$result -> free_result();
}
// if there are more result-sets, the print a divider
if ($mysqli -> more_results()) {
printf("-------------\n");
}
//Prepare next result set
} while ($mysqli -> next_result());
}
$mysqli -> close();
?>
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";
// Execute multi query
if ($mysqli -> multi_query($sql)) {
do {
// Store first result set
if ($result = $mysqli -> store_result()) {
while ($row = $result -> fetch_row()) {
printf("%s\n", $row[0]);
}
$result -> free_result();
}
// if there are more result-sets, the print a divider
if ($mysqli -> more_results()) {
printf("-------------\n");
}
//Prepare next result set
} while ($mysqli -> next_result());
}
$mysqli -> close();
?>
查看底部的程序样式示例。
定义和用法
multi_query() / mysqli_multi_query() 函数执行一个或多个针对数据库的查询。多个查询用分号进行分隔。
语法
面向对象的风格:
$mysqli ->
multi_query(query)
程序风格:
mysqli_multi_query(connection, query)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
query | 必需。规定一个或多个查询,用分号进行分隔。 |
技术细节
返回值: | 如果第一个查询失败则返回 FALSE。 |
---|---|
PHP 版本: | 5+ |
示例 - 程序样式
执行多个针对数据库的查询:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "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($result);
}
// if there are more result-sets, the print a divider
if (mysqli_more_results($con)) {
printf("-------------\n");
}
//Prepare next result set
} while (mysqli_next_result($con));
}
mysqli_close($con);
?>
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "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($result);
}
// if there are more result-sets, the print a divider
if (mysqli_more_results($con)) {
printf("-------------\n");
}
//Prepare next result set
} while (mysqli_next_result($con));
}
mysqli_close($con);
?>