gpt4 book ai didi

php - MYSQLi SELECT 获取对象

转载 作者:行者123 更新时间:2023-11-30 01:21:09 25 4
gpt4 key购买 nike

    if ( isset($_POST['update']) ){
$db=mysqli_connect("localhost","****","****","****");
$lasttime = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
while (1){
sleep(1);
clearstatcache();
$mresult = mysqli_query($db,"SELECT * FROM tblchat WHERE msg_datetime > $lasttime");
if (!empty($mresult)){ break; }
}
$msgs = array();
while ($row = mysqli_fetch_object( $mresult )) { $msgs[] = $row; }
mysqli_free_result($mresult);
$response = array();
$response['msgs'] = $msgs;
echo json_encode($response);
flush();
mysqli_close($db);
exit();
}

该代码是服务器与客户端进行长轮询连接的代码。如果请求更新,则 while 循环会检查在随更新请求一起发送的时间戳之后收到的任何新消息。如果找到,它将结果放入数组中并将其回显给客户端。结果输出类似于这样 [msgs:[{msg_from:"",msg_to:"",msg:"",msg_datetime:""},{msg_from:"",msg_to:"",msg:"",msg_datetime:""}]]

代码第一次工作正常,并发送封装良好的所有最近消息,但随后它再次发送空消息数组。请指导我。

最佳答案

解决了mysqli_num_rows的问题

if (mysqli_num_rows($mresult)){ $msgs = array(); while ($row = mysqli_fetch_object( $mresult )) { $msgs[] = $row; } mysqli_free_result($mresult); break; }
if (mysqli_num_rows($wresult)){ $writers = array(); while ($row = mysqli_fetch_object( $wresult )) { $writers[] = $row; } mysqli_free_result($wresult); break; }

感谢大家的帮助!

关于php - MYSQLi SELECT 获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com