如何遍历包含MySQL返回数据的PHP数组? [英] How do I loop through a PHP array containing data returned from MySQL?
问题描述
好的,我有一个包含几个字段的表.字段之一是username
. username
在很多情况下都是相同的,例如:
Ok I have a table with a few fields. One of the fields is username
. There are many times where the username
is the same, for example:
-
- 用户名:bob
- 密码:bob
- 报告:1
- username: bob
- password: bob
- report: 1
- 用户名:bob
- 密码:bob
- 报告:2
我对select * where username='bob';
做了一条SQL语句,但是当我执行以下PHP函数时,它只会返回最后一个结果:
I did a SQL statement to select * where username='bob';
but when I do the following PHP function, it will only return the last result:
$thisrow = mysql_fetch_row($result);
我需要从每一行中获取每个字段.我应该怎么做呢?
I need to get every field from every row. How should I go about doing this?
$mainsection="auth"; //The name of the table
$query1="select * from auth where username='$user'";
$result = mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1); //do the query
$thisrow=mysql_fetch_row($result);
echo "Study: " . $thisrow[1] . " - " . $thisrow[5];
很抱歉提出这样一个愚蠢的问题.我似乎无法让一个领域的while
循环在我的生命中发挥作用.
Sorry for such a dumb question. I can't seem to get the while
loops of more than one field working for the life of me.
推荐答案
mysql_fetch_row
一次获取每一行.为了检索多行,您将使用while
循环,如下所示:
mysql_fetch_row
fetches each row one at a time. In order to retrieve multiple rows, you would use a while
loop like this:
while ($row = mysql_fetch_row($result))
{
// code
}
这篇关于如何遍历包含MySQL返回数据的PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!