为什么我的foreach循环无法按预期工作? [英] Why is my foreach loop not working as expected?

查看:101
本文介绍了为什么我的foreach循环无法按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP开发还很陌生,但是我想我很快就开始学习它,但是当我从XAMPP迁移到真正的主机时遇到了一个问题.我正在尝试做这样的事情.

I'm pretty new to PHP development, but I'd like to think I'm picking it up at a rapid pace, but I hit a bit of a problem when I moved from XAMPP to a real host. I'm trying to do something like this.

$cast_list = mysqli_query($dblink, $sql);
foreach ($cast_list as $role)
{
    echo "<tr><td width='50%'>".$role['appeared_as']."</td>";
}

它可以在我已经安装在家用PC上的XAMPP上运行,但是不能在我要测试的主机上运行.实际的查询工作正常,所以这不是问题.当我按如下所示更改代码时,可以在PHPMyAdmin上,XAMPP上看到正确的结果.

It works on XAMPP which I have installed on my home PC, but it does not work on the hosting I've arranged to test with. The actual query is working perfectly, so that's not the problem. I can see the correct results in PHPMyAdmin, on XAMPP and when I change the code as follows.

$cast_list = mysqli_query($dblink, $sql);
$y = mysqli_num_rows($cast_list);
for ($x = 0; $x <$y; $x++)
{
    $role = mysqli_fetch_assoc($cast_list);
    echo "<tr><td width='50%'>".$role['appeared_as']."</td>";
}

第二个代码块将产生所需的效果.第一个代码块显然将迭代5次,但将不包含任何有意义的数据.实际上,第一个代码块的var_dump($role)表示它是NULL.

The second code block will produce the desired effect. The first code block will apparently iterate 5 times, but will not contain any meaningful data. In fact a var_dump($role) for the first code block reveals that it is NULL.

如果需要的话,我可以适应这种情况,但是foreach对于我来说不能正常工作是有逻辑的原因吗?

I can just adapt to this if need be, but maybe there's a logical reason why foreach isn't working properly for me?

推荐答案

mysqli_query()不返回可与foreach()一起使用的数组或数组对象. mysqli_query()的返回类型是一种资源.您可以像第二个解决方案一样,以循环方式从中获取数据.

mysqli_query() doesn't return an array or array object that you can use with foreach(). The return type of mysqli_query() is a resource. You fetch from it in a loop, like your second solution.

使用while()代替for()更简单:

It's simpler to use while() instead of for():

$cast_list = mysqli_query($dblink, $sql);
while ($role = mysqli_fetch_assoc($cast_list)) {
    echo "<tr><td width='50%'>".$role['appeared_as']."</td>";
}

当在结果集的末尾获取的行为NULL时,循环将自动终止.您无需知道循环之前的行数.

The loop will terminate automatically when the row fetched is NULL at the end of the result set. You don't need to know the number of rows before the loop.

发表您的评论

查找一些事实后,我不得不承认我的上面的答案并不完全正确.或对于某些版本的PHP并非如此.

After looking up some facts, I have to admit that my answer above isn't fully true. Or isn't true for some versions of PHP.

在PHP 5.4中,添加了Iterator功能的mysqli_result资源,您实际上可以foreach()中使用它.但是您的主机显然使用了旧版本的PHP.

In PHP 5.4, a mysqli_result resource added Iterator functionality, you actually can use it in a foreach(). But your host apparently uses an older version of PHP.

最佳做法是在将要部署到的相同版本的 all 软件上进行开发,这样您就不会为此类惊喜感到惊讶了.

The best practice is to develop on the same version of all software that you will deploy to, so you aren't caught by this sort of surprise.

这篇关于为什么我的foreach循环无法按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆