php 中的 mysql_fetch_array 和 while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17023028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:07:45  来源:igfitidea点击:

mysql_fetch_array and while loop in php

phpmysql

提问by Braggae

I'm trying to get data from DB and print it on the page. I use next select:

我正在尝试从数据库获取数据并将其打印在页面上。我使用下一个选择:

$tableIndex=mysql_query('SELECT table_index FROM table_names');

When I use a whileloop to print, it's ok. Code:

当我使用while循环打印时,没关系。代码:

while($row = mysql_fetch_array($tableIndex) ){
        echo $row[0].'<br>';}

Result:

结果:

1st

2nd

3rd

But without whileloop, it gives me just first element of array.

但是没有while循环,它只给我数组的第一个元素。

Code:

代码:

$row = mysql_fetch_array($tableIndex);
    echo $row[0].'<br>';
    echo $row[1].'<br>';
    echo $row[2].'<br>';

Result:

结果:

1st

_

_

(where "_" is blank space)

(其中“_”是空格)

Can someone expalin why it works so strangely?

有人可以解释为什么它的工作如此奇怪吗?

回答by Spudley

You're fundamentally misunderstanding how these things work.

你从根本上误解了这些东西是如何工作的。

The "array" in mysql_fetch_arrayis not an array of all the records, but an array of the data within the current record.

中的“数组”mysql_fetch_array不是所有记录的数组,而是当前记录中数据的数组。

In your case, you're only fetching a single field, so the array will only contain a single element (ie $row[0]), but the principle is the same -- it's an array of the single record that you have just read.

在您的情况下,您只获取一个字段,因此该数组将只包含一个元素(即$row[0]),但原理是相同的 - 它是您刚刚读取的单个记录的数组。

Only the data for the current record is in the array at any one time. That's what the whileloop is for; it goes back any loads each record one after the other.

任何时候只有当前记录的数据在数组中。这就是while循环的用途;它一个接一个地返回任何加载每个记录。

If you want to create an array that contains all the data, you need to do it like this:

如果你想创建一个包含所有数据的数组,你需要这样做:

$fullData = array()
while($row = mysql_fetch_array($tableIndex) ){
    $fullData[] = $row[0];
}

That will put all the data that you're reading into a single big array, which is what you're expecting. Now you can do what you wanted to do in the question:

这会将您正在读取的所有数据放入一个大数组中,这正是您所期望的。现在您可以在问题中做您想做的事情:

echo $fullData[0].'<br>';
echo $fullData[1].'<br>';
echo $fullData[2].'<br>';

Hope that helps.

希望有帮助。

It's worth pointing out here that the mysql_xxx()family of functions are deprected and considered obsolete. If you're just learning PHP (which would seem to be the case), I strongly recommend that you stop learning these functions, and learn the PDOlibrary instead. It is more modern and has a lot of features that the mysqlfunctions can't provide. In addition, future versions of PHP will remove the mysqlfunctions entirely, so you'll have to switch at some point -- it may as well be now, while you're still learning.

值得在这里指出的是mysql_xxx(),函数系列已被弃用并被认为已过时。如果您只是在学习 PHP(情况似乎如此),我强烈建议您停止学习这些函数,PDO而是学习库。它更现代,并具有许多mysql功能无法提供的功能。此外,PHP 的未来版本将mysql完全删除这些函数,因此您必须在某个时间点进行切换——现在也可能是在您仍在学习的时候。

Also (to keep things relevant to the question), the PDO library has a feature that does in fact do what you're looking for in a single function: PDO::fetchAll(). Using this method means that you canfetch all the data into a single big array in one line with no need to do a while loop. The code would look a bit like this:

此外(为了保持与问题的相关性),PDO 库有一个功能,它实际上可以在单个函数中完成您正在寻找的功能:PDO::fetchAll(). 使用这种方法意味着您可以在一行中将所有数据提取到一个大数组中,而无需执行 while 循环。代码看起来有点像这样:

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetchAll();

(example taken from the PHP manual for PDO::fetchAll)

(示例取自 PHP 手册PDO::fetchAll

回答by Evert

Every time your loop loops around, this code is executed again:

每次循环循环时,都会再次执行此代码:

$row = mysql_fetch_array($tableIndex);

So every time $row gets the new value. The equivalent without a loop would be:

所以每次 $row 获得新值。没有循环的等价物是:

$row = mysql_fetch_array($tableIndex);
echo $row[0].'<br>';
$row = mysql_fetch_array($tableIndex);
echo $row[0].'<br>';
$row = mysql_fetch_array($tableIndex);
echo $row[0].'<br>';