在PHP中,fetch_array()
函數(shù)用于從結(jié)果集中獲取一行作為數(shù)組
while
循環(huán):$result = mysqli_query($connection, $sql);
if ($result) {
while ($row = mysqli_fetch_array($result)) {
// 處理每一行數(shù)據(jù)
echo "ID: " . $row['id'] . " - Name: " . $row['name'] . "<br>";
}
} else {
echo "查詢失敗";
}
foreach
循環(huán):$result = mysqli_query($connection, $sql);
if ($result) {
foreach (mysqli_fetch_all($result, MYSQLI_ASSOC) as $row) {
// 處理每一行數(shù)據(jù)
echo "ID: " . $row['id'] . " - Name: " . $row['name'] . "<br>";
}
} else {
echo "查詢失敗";
}
注意事項(xiàng):
mysqli_prepare
)以提高安全性并防止SQL注入。mysqli_free_result()
釋放結(jié)果集內(nèi)存。