好吧,我有一个几个领域的表。其中一个领域是username
。有很多次,其中username
是一样的,例如:如何循环包含从MySQL返回的数据的PHP数组?
-
- 用户名:鲍勃
- 密码:鲍勃
- 报告:1
-
- 用户名:鲍勃
- 密码:鲍勃
- 报告:2
我做了一个SQL语句select * where username='bob';
但是当我做下面的PHP函数,它只会返回最后结果:
$thisrow = mysql_fetch_row($result);
我需要得到各个领域来自每一行。我应该如何去做这件事?
$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
循环为我的生活工作。
当心SQL注入与$用户的。使用mysql_real_escape_string($ text)来确保特殊字符被转义。此外,mysql_fetch_assoc($ result)会为您提供一个命名数组 - 即$ thisrow ['name_of_database_field'] - 从长远来看,使用起来更容易。 – ceejayoz 2009-04-24 21:48:15