【问题标题】:Mysql table using LIMIT, breaks randomly使用 LIMIT 的 Mysql 表,随机中断
【发布时间】: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 = On in php.ini
  • 我们想要“全部”以及 LIMIT 但是 LIMIT 在 100 个用户之后不起作用?
  • 警告:如果您只是学习 PHP,请不要学习过时的 mysql_query 接口。它很糟糕,并且在 PHP 的未来版本中被删除。像PDO is not hard to learn 这样的现代替代品。像PHP The Right Way 这样的指南可以帮助解释最佳实践。始终绝对确定您的用户参数是properly escaped,否则您将遇到严重的SQL injection bugs

标签: php mysql arrays json


【解决方案1】:

您不应该使用mysql_connect,因为它已被弃用。请改用mysqli_connect。另外,我认为您不需要进行太多迭代,只需在 select 语句中选择您想要的内容即可。

更新:导致这种情况发生的另一个因素也可能是从数据库返回的信息,如果您的字符串中有撇号 ' 并且您尝试使用 json_encode,它会中断,您需要addslashes先。

尝试以下操作,根据需要更改您的 LIMIT,如果仍然有这些错误,请告诉我:

<?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();
?>

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 如果可能的话,您能否随时通过团队查看器给我一些时间...... @CodeGodie
【解决方案2】:

首先,在您的 INI 或脚本中启用错误报告:

 <?php error_reporting(E_ALL);
       ini_set('display_errors', 1);

如果在内存分配不足或类似情况下出现任何错误/警告,这将为您提供帮助。

要获取所有记录,您不应使用 LIMIT,请从 SQL 中删除 LIMIT。

select * from users

【讨论】:

  • 他们想限制 5000,所以他们需要一个 LIMIT
  • @CodeGodie - 它在哪里说他们想要一个限制?他们目前正试图应用一个上限,但正如问题所说,想要“全部”:How we get __all__ records from mysql table?
  • 我们想要“全部”以及 LIMIT 但是 LIMIT 在 100 个用户之后不起作用?
  • 这就是他想要的吗?他提到他试图将限制提高到 5000,但他没有收到任何东西。在那种情况下,他希望能够将限制一直提高到 5000,甚至在某个时候达到 6000。 @Nixi,编辑你的标题,以免用户感到困惑。
  • 如果用户需要所有记录,为什么要添加限制?
猜你喜欢
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 2011-03-02
  • 2012-03-04
  • 2012-08-04
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多