如何将mysql_query中的所有行添加到mysql_fetch_array()?我希望能够尽可能高效地完成它,因为它可以处理很多行。
答案 0 :(得分:15)
最常见的方式:
$rows = array();
while(($row = mysql_fetch_array($result))) {
$rows[] = $row;
}
答案 1 :(得分:11)
<?php
$db = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);
$startTime = microtime(true);
$result = $db->query("select * from users");
$users = $result->fetch_all(MYSQLI_ASSOC); // faster
// while($row = $result->fetch_assoc()) $users[] = $row; //slower
echo sprintf("%d users fetched in %s secs",
count($users), number_format(microtime(true) - $startTime, 6, ".", ""));
$db->close();
?>
编辑一些运行时统计信息
$users = $result->fetch_all()
1000 users fetched in 0.001462 secs
5000 users fetched in 0.005493 secs
15000 users fetched in 0.015517 secs
50000 users fetched in 0.051950 secs
100000 users fetched in 0.103647 secs
VS
while($row = $result->fetch_assoc()) $users[] = $row;
1000 users fetched in 0.001945 secs
5000 users fetched in 0.008101 secs
15000 users fetched in 0.023481 secs
50000 users fetched in 0.081441 secs
100000 users fetched in 0.163282 secs
有趣:)