How I can execute many query in one page?

2019-03-07 10:04发布

I write this code but when I run it no output.There is no error but the problem no output. Can you help me? or How I can execute many query 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> 

3条回答
再贱就再见
3楼-- · 2019-03-07 10:28

Just run your queries one by one using regular mysqli_query.

Leave multi_query alone, it is not for your case.

查看更多
戒情不戒烟
4楼-- · 2019-03-07 10:34

First: there is an error. You are missing a where in the first query:

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

has to be:

$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

What I wonder is if you really configured your server to execute php inside html.

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);
?>

I added the missing where and the closure of connection.

By the way, I hope you are sure the records with those id exist in your DB.

Regards

查看更多
登录 后发表回答