因为mysql_fetch_assoc
一次只返回一行,所以使用它将 PHP 结果集转换为 PHP 数组。然后,在数组上使用 foreach。
例如:
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
foreach($rows as $row) {
//Then do something for each row, for e.g.:
if ($row['col_name'] == 'somevalue'){
$arrList[] = $row['id'];
}
}
或者:
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
foreach($rows as $row) {
foreach($row as $field => $value) {
//do something with $field and $val
}
}
或者:
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
for($i=0;$i<count($rows);$i++) {
//do something with $rows[$i]['field_name']
}
来源:
Devshed:PHP foreach/while mysql 结果?