gpt4 book ai didi

PHP while 循环返回空结果

转载 作者:行者123 更新时间:2023-11-29 14:25:31 25 4
gpt4 key购买 nike

$connect_status = "SELECT connected_to_id FROM tbl_connect WHERE user_id = 
{$_SESSION['user_id']}";

$exec_connstatus_query = mysql_query($connect_status, $db_connect);

while($check_status = mysql_fetch_array($exec_connstatus_query))
{
if ($check_status['connected_to_id'] == $profileid)
{
echo "{$check_status['connected_to_id']}";
}
else
{
echo "fail";
}
}

这段代码已经困扰了我 3 个小时。即使在这种情况下,问题仍然存在:

if ($check_status['connected_to_id'] == $profileid)

返回 false,当 else 语句执行时我看不到“失败”。有什么想法吗?

最佳答案

mysql_query() 出错时返回 FALSE,并且循环根本不会执行。如果 $_SESSION['user_id'] 不是数字,就会发生这种情况。为什么不只是

$connect_status = "SELECT connected_to_id FROM tbl_connect WHERE user_id ='$_SESSION[user_id]'";

如果没有(更多)行,则 mysql_fetch_array() 返回 FALSE。如果 tbl_connect 中不存在您的 session ,则循环将不会执行。难道你不想要这样的东西:

$connect_status = "SELECT connected_to_id FROM tbl_connect 
WHERE user_id ='$_SESSION[user_id]' LIMIT 1";
$exec_connstatus_query = mysql_query($connect_status, $db_connect);
if( ($check_status = mysql_fetch_array($exec_connstatus_query)) &&
($check_status['connected_to_id'] == $profileid) ) {
echo "{$check_status['connected_to_id']}";
} else {
echo "fail";
}

关于PHP while 循环返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11054042/

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