php 不能显示mysql,PHP不显示MYSQL查询的结果

I have the following code

$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;

}

}

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

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result

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.

解决方案

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值