【问题标题】:Getting data from MYSQL into JSON using PHP使用 PHP 将数据从 MYSQL 获取到 JSON
【发布时间】:2011-06-16 12:29:45
【问题描述】:

我有以下非常简单的测试 PHP 代码,用于提取数据并将其放入 JSON 格式的文本中。

我收到以下错误..

致命错误:第 33 行 /var/www/test.php 中允许的内存大小为 33554432 字节已用尽(尝试分配 1979603 字节)

第 33 行是json_encode() 行。

有没有办法提高效率? PHP.ini 已设置为最大 32M,因此从 8M 标准扩大!

 <?php
    require('../../admin/db_login.php');

    $db=mysql_connect($host, $username, $password) or die('Could not connect');
    mysql_select_db($db_name, $db) or die('');

    $result = mysql_query("SELECT * from listinfo") or die('Could not query');
    $json = array();

    if(mysql_num_rows($result)){
            $row=mysql_fetch_assoc($result);
        while($row=mysql_fetch_row($result)){
            //  cast results to specific data types

            $test_data[]=$row;
        }
        $json['testData']=$test_data;
    }

    mysql_close($db);

    echo json_encode($json);


    ?>

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    作为第一个解决方法,将其设置为 256M 甚至 512M。

    MySQL 返回给您的数据集可能非常大。因此,即使您的 PHP 非常节省内存,您仍然会收到 OoM 错误。因此,作为更可行的长期解决方案,请使用LIMIT 语句(SELECT * FROM $table WHERE 1=1 LIMIT 0,30(从索引 0 开始,获取 30 项)。

    编辑:哦,哇,我什至没有从第一个解决方案中看到问题......好吧,LIMIT 您的查询仍然可能是个好主意:-)

    【讨论】:

      【解决方案2】:

      停止复制您的数据数组

      $json = array();
      
      if(mysql_num_rows($result)){
              $row=mysql_fetch_assoc($result);
          while($row=mysql_fetch_row($result)){
              //  cast results to specific data types
      
              $json['testData'][]=$row;
          }
      }
      

      这将有助于减少您的内存使用量

      【讨论】:

      • 是的,好喊!没有意识到我在那里复制它!这已经使用了 16 个字节,现在设置为 64M 适合:-)
      【解决方案3】:

      您可能正在编码一个非常大的数据集。您可以一次对每一行进行编码,而不是在一个大操作中对其进行编码。

      <?php
      require('../../admin/db_login.php');
      
      $db=mysql_connect($host, $username, $password) or die('Could not connect');
      mysql_select_db($db_name, $db) or die('');
      
      $result = mysql_query("SELECT * from listinfo") or die('Could not query');
      
      if(mysql_num_rows($result)){
          echo '{"testData":[';
      
          $first = true;
          $row=mysql_fetch_assoc($result);
          while($row=mysql_fetch_row($result)){
              //  cast results to specific data types
      
              if($first) {
                  $first = false;
              } else {
                  echo ',';
              }
              echo json_encode($row);
          }
          echo ']}';
      } else {
          echo '[]';
      }
      
      mysql_close($db);
      

      这样,每次调用json_encode() 只编码一个小数组而不是一个大数组。最终结果是一样的。 这是 IMO 使用较少内存的解决方案。

      【讨论】:

      • @Lee:非常规,但这样您就不必将每一行存储到一个数组中以供以后编码。
      • 有没有办法让它把行标题放在每一个上?
      • @Lee:每一行都有"testData"?是的,但你为什么要这样?这不是您的原始代码的行为方式。
      • 否,要有列标题:列数据
      • @Lee:当然!在您的 while 循环中将 mysql_fetch_row($result) 替换为 mysql_fetch_array($result, MYSQL_ASSOC)
      【解决方案4】:

      使用这个:

      $result = mysql_query("SELECT * FROM listinfo");
      
      $json = array();
      $total_records = mysql_num_rows($result);
      
      if($total_records > 0){
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
          $json[] = $row;
        }
      }
      
      echo json_encode($json);
      

      【讨论】:

      • 你为什么使用if($total_records &gt;= 1){$total_records = mysql_num_rows($result);
      • 一些解释会很好
      【解决方案5】:

      这是我的第一个完美运行的 json

      <?php
      // connect to mysql server
      mysql_connect($host, $username, $password) or die('Could not connect');
      // select the db name
      mysql_select_db($dbname);
          // enter your sql query
          $sql = "Select * from Order_Details";
      // Creates temp array variable
      $temp = array();
      // Gets table details
      $result = mysql_query($sql);
      // Adds each records/row to $temp
      while($row=mysql_fetch_row($result)) {
          $temp[] = $row;
      }
      // Formats json from temp and shows/print on page
      echo json_encode($temp);
      ?>
      

      【讨论】:

        猜你喜欢
        • 2016-09-22
        • 2014-05-07
        • 1970-01-01
        • 2018-01-14
        • 2012-01-22
        • 2012-10-21
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        相关资源
        最近更新 更多