当我运行下面的代码时,它将从一个表中返回100个用户记录,但当我将LIMIT的值从100增加到一个更大的数字时,例如5000,它将不会返回任何内容。那么,我如何访问不同数量的记录,如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);
?>发布于 2015-03-31 22:30:25
首先,在INI或脚本中启用错误报告:
<?php error_reporting(E_ALL);
ini_set('display_errors', 1);这将帮助您在内存分配不足或类似情况下出现任何错误/警告。
要获取所有记录,您不应该使用LIMIT,从SQL中删除LIMIT。
即select * from users
发布于 2015-03-31 22:54:29
您不应该使用mysql_connect,因为它已被弃用。请改用mysqli_connect。此外,我认为您不需要迭代太多,只需在select语句中选择所需内容即可。
更新:发生这种情况的另一个因素也可能是从数据库返回的信息,如果你的字符串中有撇号',而你试图使用json_encode,它将会中断,你需要首先使用addslashes。
尝试以下操作,根据需要更改您的限制,并让我知道您是否仍然存在这些错误:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT sno,mnc,mcc,lac,cell_id,lat,lng,address FROM users LIMIT 100";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$response = array();
while ($row = $result->fetch_assoc()) {
foreach($row as $k => $str){
$row[$k] = addslashes($str);
}
array_push($response, $row);
}
echo json_encode($response);
} else {
echo "0 results";
}
$conn->close();
?>https://stackoverflow.com/questions/29371039
复制相似问题