Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

mysql_fetch_array的問題

我想請問while裡的條件 $row = mysql_fetch_array($result)

是什麼意思呢?

<?

$hostname=\"localhost\";

$username=\"root\";

$pwd=\"11111\";

$dbname=\"test\";

$link=mysql_connect($hostname,$username,$pwd);

if(!$link){

echo \"無法建立主機連線\";

}

$ssql=mysql_query(\"use $dbname\");

//可寫成$ssql=mysql_select_db($dbname);

if(!$ssql){

echo \"無法連接資料庫\";

}

mysql_query(\"insert post values(\'$_POST[id]\',NOW(),\'$_POST[name]\',\'$_POST[email]\',\'$_POST[message]\')\");

$result = mysql_query(\"SELECT * FROM post order by date desc\");

echo \"<table border=1 width=70%>\";

$i=mysql_num_rows($result);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo \"

<tr>

<td>$i</td>

<td>$row[date]</td>

</tr>

<tr>

<td>姓名: $row[name]</td>

<td>email:$row[email]</td>

</tr>

<tr><td colspan=2> 留言內容:$row[message]</td></tr>\";

$i--;

}

echo \"</table>\";

mysql_free_result($result);

mysql_close($link);

?>

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    mysql_fetch_array( $result )的功用是取得$result傳回資訊的資料,而$result的資訊則可由mysql_query()來檢索資料庫後取得

    所以說,while ($row = mysql_fetch_array($result))的意思就是若$row指定為mysql_fetch_array($result)所傳回的值後若$row內容不為空時則執行while()區間內的敘述,區間執行結束後返回,並將mysql_fetch_array的指標指向下一筆數據

    Source(s): 我的頭腦
  • 1 decade ago

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    這行可直接寫成...

    while ($row = mysql_fetch_assoc($result)) {

Still have questions? Get your answers by asking now.