同樣注意:mysql_* 已移除,請改用 mysqli/PDO。mysql_fetch_assoc 返回關聯陣列(欄位名為鍵),比數值索引更易讀。
mysql_fetch_assoc(resource $result): array|false
$result = mysql_query("SELECT ISBN, Title, Author FROM articles", $link);
while ($row = mysql_fetch_assoc($result)) {
echo "ISBN = {$row['ISBN']}<br>\n";
echo "Title = {$row['Title']}<br>\n";
echo "Author = {$row['Author']}<br>\n";
}
$stmt = $pdo->query('SELECT ISBN, Title, Author FROM articles');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "ISBN = {$row['ISBN']}<br>\n";
// ...
}
$res = $mysqli->query('SELECT ISBN, Title, Author FROM articles');
while ($row = $res->fetch_assoc()) { /* ... */ }