MYSQL-从读取的数组中选择特定值 [英] MYSQL - Select specific value from a fetched array

查看:488
本文介绍了MYSQL-从读取的数组中选择特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,由于我对所有这些东西都还很陌生,因此我无法成功对其进行谷歌搜索,因为我不知道我所寻找的确切定义.

I have a small problem and since I am very new to all this stuff, I was not successful on googling it, because I dont know the exact definitions for what I am looking for.

我有一个非常简单的数据库,并由此获得了所有行:

I have got a very simple database and I am getting all rows by this:

while($row = mysql_fetch_array($result)){
    echo $row['id']. " - ". $row['name'];
    echo "<br />";
}

现在,我的问题是:如何过滤第二个结果?我以为这样的事情可能行得通,但是不行:

Now, my question is: how do I filter the 2nd result? I thought something like this could work, but it doesnt:

$name2= $row['name'][2];

有可能吗?还是我必须编写另一个mysql查询(如SELECT .. WHERE id = "2"之类)以在第二行中获取名称值?

Is it even possible? Or do I have to write another mysql query (something like SELECT .. WHERE id = "2") to get the name value in the second row?

我要遵循的是:

-从数据库中获取所有数据(使用"while循环"),但是要在我的页面上单独显示某些结果.例如echo("name in second row")echo("id of first row")等等.

-get all data from the database (with the "while loop"), but than individually display certain results on my page. For instance echo("name in second row") and echo("id of first row") and so on.

推荐答案

如果您希望使用完整的结果集而不是只遍历一次,则可以将整个结果集放入数组:

If you would rather work with a full set of results instead of looping through them only once, you can put the whole result set to an array:

$row = array();

while( $row[] = mysql_fetch_array( $result ) );

现在,您可以使用第一个索引访问单个记录,例如,第二行的名称字段在$row[ 2 ][ 'name' ]中.

Now you can access individual records using the first index, for example the name field of the second row is in $row[ 2 ][ 'name' ].

这篇关于MYSQL-从读取的数组中选择特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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