【发布时间】:2015-06-04 22:25:07
【问题描述】:
当我运行以下代码时,它将从表中返回 100 条用户记录,但是当我将 LIMIT 的值从 100 增加到更大的数字(如 5000)时,它不会返回任何内容。我在表中总共有 6000 条记录。那么如何访问不同数量的记录,例如 2000、3000 甚至全部 6000 条记录?请指导有什么问题!
<?php
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect to db: ' . mysql_error());}
mysql_select_db("distributedsms",$db);
$result = mysql_query("select * from users LIMIT 100", $db);
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['user no'] = $row['sno'];
$row_array['mnc'] = $row['mnc'];
$row_array['mcc'] = $row['mcc'];
$row_array['lac'] = $row['lac'];
$row_array['cell id'] = $row['cell id'];
$row_array['lat'] = $row['lat'];
$row_array['lng'] = $row['lng'];
$row_array['address'] = $row['address'];//push the values in the array
array_push($json_response,$row_array);
$users = $json_response;}
$fp = fopen('users_data.json', 'w+');
fwrite($fp, json_encode($json_response));
fclose($fp);
echo json_encode($json_response);
?>
【问题讨论】:
-
大部分循环代码是没有意义的。为什么不只是
while($row = ...) { $json_response[] = $row }? -
如果它没有返回任何内容,则可能是代码超时或达到内存限制....检查您的服务器日志
-
当你在开发的时候转
display_errors = Oninphp.ini -
我们想要“全部”以及 LIMIT 但是 LIMIT 在 100 个用户之后不起作用?
-
警告:如果您只是学习 PHP,请不要学习过时的
mysql_query接口。它很糟糕,并且在 PHP 的未来版本中被删除。像PDO is not hard to learn 这样的现代替代品。像PHP The Right Way 这样的指南可以帮助解释最佳实践。始终绝对确定您的用户参数是properly escaped,否则您将遇到严重的SQL injection bugs。