如何在一页中执行多个查询? [英] How I can execute many queries in one page?
问题描述
我写了这段代码,但是当我运行它时没有输出.没有错误但问题没有输出.
I write this code but when I run it no output. There is no error but the problem no output.
如何在同一个页面中执行多个查询?
How I can execute many queries in the same 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:
First: there is an error. You are missing a where in the first query:
$query = "SELECT * FROM `student_record` id = 201102887;";
必须是:
$query = "SELECT * FROM `student_record` where id = 201102887;";
这足以有一个空白屏幕.
That is sufficient to have a blank screen.
其余的你的代码没问题,符合以下经典示例:
For the rest your code is ok, in line with the classical example from:
http://php.net/manual/en/mysqli.multi-query.php
我想知道您是否真的将服务器配置为在 html 中执行 php.
What I wonder is if you really configured your server to execute php inside html.
将这部分代码与上述更正放在一个 .php 文件中,您将看到结果:
Put this part of your code with the above correction in a .php file and you will see results:
<?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);
?>
我添加了缺少的 where 和连接的关闭.
I added the missing where and the closure of the connection.
顺便说一句,我希望您确定具有这些 ID 的记录存在于您的数据库中.
By the way, I hope you are sure the records with those ids exist in your DB.
这篇关于如何在一页中执行多个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!