gpt4 book ai didi

php - 如果 PHP 中的数据库的 foreach 循环没有返回任何行怎么办?

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

问题:每次我循环一个空行时,它都会显示:

Invalid argument supplied for foreach()

//return results from `users` table.
$users = DB::getInstance()->get('users');

//loop the username from the users table
foreach($users as $user) {
echo $user['username'] . "<br>";
}

最佳答案

在不知道您正在使用哪个数据库库的情况下,您可以执行类似的操作来检查是否返回了任何用户。

if (is_array($users) && count($users) > 0) {
foreach ($users as $user) {
echo $user['username'] . "<br>";
}
}

如果您提供更多详细信息,我将能够给您更好的答案。

关于php - 如果 PHP 中的数据库的 foreach 循环没有返回任何行怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618168/

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