好吧,我有一個幾個領域的表。其中一個領域是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