mysql_fetch_array和php中的while循环 [英] mysql_fetch_array and while loop in php

查看:121
本文介绍了mysql_fetch_array和php中的while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我使用while循环进行打印时,就可以了. 代码:

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

结果:

1st

2nd

3rd

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

代码:

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

结果:

1st

_

_

(其中"_"为空格)

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

解决方案

您从根本上误解了这些方法的工作原理.

mysql_fetch_array中的数组"不是所有记录的数组,而是当前记录中的数据数组.

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

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

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

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

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

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

希望有帮助.

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

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

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

(摘自PHP手册中的 PDO::fetchAll 的示例)

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 while loop to print, it's ok. Code:

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

Result:

1st

2nd

3rd

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

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?

解决方案

You're fundamentally misunderstanding how these things work.

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

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.

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

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 PDO library instead. It is more modern and has a lot of features that the mysql functions can't provide. In addition, future versions of PHP will remove the mysql functions entirely, so you'll have to switch at some point -- it may as well be now, while you're still learning.

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 can fetch 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:

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

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

这篇关于mysql_fetch_array和php中的while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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