我如何在一个页面中执行许多查询


How I can execute many queries in one page?

我写了这段代码,但当我运行它时没有输出。没有错误,但问题没有输出。

如何在同一页面中执行多个查询?

<html>
<head>
<title> tran </title>
</head>
<body>
<?php
$link = mysqli_connect('localhost', 'root', '', 'uoh');
$query = "SELECT * FROM `student_record` id = 201102887;";
$query .= "SELECT * FROM `course` where id = 201102887;"; 
if (mysqli_multi_query($link, $query)) {
do {
    if ($result = mysqli_store_result($link)) {
        while ($row = mysqli_fetch_array($result)) 
{
echo $row['code'];
echo $row['term'];
}
}   
} while (mysqli_next_result($link));
}
?>
</body>
</html> 

首先:有错误。您在第一个查询中缺少 where:

$query = "SELECT * FROM `student_record` id = 201102887;";

必须是:

$query = "SELECT * FROM `student_record` where id = 201102887;";

这足以有一个空白屏幕。

对于其余部分,您的代码还可以,与经典示例一致:

http://php.net/manual/en/mysqli.multi-query.php

我想知道的是你是否真的配置了你的服务器在 html 中执行 php。

将上述更正的这部分代码放在一个.php文件中,您将看到结果:

<?php
$link = mysqli_connect('localhost', 'root', '', 'uoh');
$query = "SELECT * FROM `student_record` where id = 201102887;";
$query .= "SELECT * FROM `course` where id = 201102887;"; 
    if (mysqli_multi_query($link, $query)) {
        do {
            if ($result = mysqli_store_result($link)) {
                while ($row = mysqli_fetch_array($result)) {
                    echo $row['code'];
                    echo $row['term'];
                }
            }   
        } while (mysqli_next_result($link));
    }
/* close connection */
mysqli_close($link);
?>

我添加了缺少的位置和连接的关闭。

顺便说一下,我希望您确定数据库中存在具有这些 ID 的记录。

只需使用常规mysqli_query逐个运行查询即可。

别管multi_query,这不适合您的情况。