PHP不显示MYSQL查询的结果 [英] PHP not displaying result from MYSQL query
问题描述
我有以下代码
$sql = "SET @uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);";
$sql = "UPDATE channels SET Used = 1 WHERE ID = @uid;";
$sql = "SELECT * FROM channels WHERE ID IN = @uid;";
$result = mysqli_multi_query($conn, $sql)
or die( mysqli_error($sql) );
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
现在我遇到的问题是php is not显示由MYSQL查询返回的结果,该结果存储在稍后要存储在虚拟页面中的代码中的会话中,它出现以下错误
Now the issue i'm having is the php isnt displaying the result returned by the MYSQL query which is stored in a session later on in the code to be displayed on a dummy page it comes up with the following error
警告:mysqli_num_rows()期望参数1为mysqli_result
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
我的SQL查询完全可以满足我的需要,所以我真的不想更改它.我只需要一些有关如何使PHP回显@uid的建议 有谁愿意帮助我解决这个问题?如果是这样,谢谢.
The my SQL query does exactly what I need it to I just need to, so I don't really want to change it. I just need some advice on how i'd get the PHP to echo the @uid is there anyone willing to help me solve the issue? if so thankyou.
推荐答案
在$ sql中有3个查询,因此应使用multi_query函数 http://php.net/manual/en/mysqli.multi-query.php
You have 3 queries in your $sql so you should use multi_query function http://php.net/manual/en/mysqli.multi-query.php
您可以将第一个查询更改为:
And you can change your first query to:
SET @uid = 0;
SELECT @uid := ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
更新,您可以尝试对这段代码进行修改,并添加所有已注释的改进.
Update You can try this fragment of your code modified with all commented improvements.
$sql = 'SET @uid = 0;';
$sql .= 'SELECT @uid:= ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);';
$sql .= 'UPDATE channels SET Used = 1 WHERE ID = @uid;';
$sql .= 'SELECT * FROM channels WHERE ID IN = @uid;';
if (mysqli_multi_query($conn, $sql)) {
do {
$result = mysqli_store_result($conn);
} while(mysqli_next_result($conn));
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
} else {
die( mysqli_error($conn) );
}
这篇关于PHP不显示MYSQL查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!