定义

mysqli_multi_query()函数执行一个或多个查询,用数据库的分号分隔。

 

语法

面向对象的风格:

bool mysqli::multi_query ( string $query )

程序风格:

bool mysqli_multi_query ( mysqli $  , string $query )

 

参数

参数 是必须的 描述
需要。 MySQL连接
query 需要。 一个或多个查询用分号分隔

 

返回值

如果查询失败,则返回FALSE。

 

实例1

以下代码针对数据库执行多个查询。

<?php
//  http://www.manongjc.com/article/1683.html
//  作者:码农教程
$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);
?>

 

实例2

<?php
//  http://www.manongjc.com/article/1683.html
//  作者:码农教程
$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();
?>
收藏 打印