我有一个现有的数据库,它有一个名为PERSON的表和一个名为NAME的字段。我要做的是选择NAME为"bill"的表中的所有行。然后,我希望将结果存储在一个数组中,我可以在稍后的时间逐步完成该数组。
现在,问题是我的代码将只选择名为"bill"的第一行,而忽略NAME为"bill"的其余行。至少在我用print_r()打印数组内容时是这样的。我的代码如下:
<?php
$getAllPreview = "SELECT * from PERSON where NAME = 'bill'";
$getAllResult = @mysql_query( $getAllPreview );
$getAllRows = @mysql_fetch_assoc( $getAllResult );
print "<pre>";
print_r($getAllRows);
print "</pre>";
?>发布于 2014-01-30 01:52:28
<?php
$getAllPreview = "SELECT * from PERSON where NAME = 'bill'";
$getAllResult = @mysql_query( $getAllPreview );
while ($row = @mysql_fetch_assoc( $getAllResult ) ) {
$getAllRows[] = $row;
}
print "<pre>";
print_r($getAllRows);
print "</pre>";
?>发布于 2014-01-30 01:52:36
while($row = mysql_fetch_array($getAllResult, MYSQL_ASSOC)) {
$data[] = $row;
}发布于 2014-01-30 01:52:59
您只需一直在mysql_fetch_assoc上循环,直到不再返回行。如果您想要输出或处理它们,只需在循环的每一次迭代中都这样做,因为它比先将其放在数组中更有效。但不管怎么说,你还是来了:
$allRows = array ();
while ($row = mysql_fetch_assoc( $getAllResult)) $allRows [] = $row;https://stackoverflow.com/questions/21446397
复制相似问题