【问题标题】:print_r showing array contents but echo json_encode is notprint_r 显示数组内容但 echo json_encode 不是
【发布时间】:2015-02-26 00:48:28
【问题描述】:

我正在尝试对以下 mysqli 结果进行 json_encode。我的问题是在将每一行添加到我的数组后,我无法“回显 json_encode”我的数组,但我能够 print_r 数组。如果我为此特定查询添加限制,则 echo json_encode 有效,但该限制为 313。我认为这表明存在 php 内存限制问题,但在将 memory_limit 更改为 256M 后,我仍然只能返回相同数量的数据。我已经尝试搜索类似的问题,但没有什么与我面临的问题足够接近。我的整个 php 脚本粘贴在下面。任何建议表示赞赏。

<?php
    if(isset($_GET["table"])){
        // Open db connection
        require "opendb.php";
        /*** $table = assay ***/
        $table = $_GET["table"];

        $query = "SELECT * FROM invitrodb_v1.{$table}";
        // Store results from query
        $data = mysqli_query($conn, $query);

        $table_row = array();

        while($row = mysqli_fetch_array($data)){
            $table_row[] = $row;
        }

        /**
        *
        *print_r displays array contents
        *echo json_encode does not
        *
        **/
        //echo json_encode($table_row);
        //print_r($table_row);

        // Close db connection
        require "closedb.php";
    }
    //***342 rows returned with the assay table when not limited***//
?>

编辑:这是我返回的示例。对于这个特定的表,有 342 行与此类似..fyi 这是来自此处的公共数据库的数据http://www.epa.gov/ncct/toxcast/data.html(以防有人好奇)。

{
    "aid": "1",
    "asid": "1",
    "assay_name": "ACEA_T47D",
    "assay_desc": "ACEA_T47D is a cell-based single readout assay using T47D human breast cell line at 80 hours in a 96-well plate.",
    "timepoint_hr": "80",
    "organism_id": "9606",
    "organism": "human",
    "tissue": "breast",
    "cell_format": "cell line",
    "cell_free_component_source": "NA",
    "cell_short_name": "T47D",
    "cell_growth_mode": "adherent",
    "assay_footprint": "microplate: 96-well plate",
    "assay_format_type": "cell-based",
    "assay_format_type_sub": "cell-based format",
    "content_readout_type": "simple",
    "dilution_solvent": "DMSO",
    "dilution_solvent_percent_max": "0.5"
}

【问题讨论】:

  • 您是否遇到任何错误?在您打开 &lt;?php 标记 error_reporting(E_ALL); ini_set('display_errors', 1); 后立即将错误报告添加到文件顶部
  • @JayBlanchard 添加后我没有收到任何错误。结果还是一样。
  • var_dump(json_encode($table_row));
  • 可能导致您有些困惑的部分原因是您使用的是更通用的mysqli_fetch_array(),它将每个字段的数字键和关联键都填充到响应数组中。您可能想要进行仅限关联的提取。除此之外,json_encode() 没有理由不工作。
  • @AbraCadaver var_dump(json_encode($table_row));仅在屏幕上显示 bool(false)。我应该像这样在我的 while 循环中调用 var_dump: $table_row[] = var_dump($row);如果我这样做,那么每一行都会无限制地显示在屏幕上。但是,如果我只在数组被填充后调用 var_dump,我什么也得不到。

标签: php arrays json mysqli


【解决方案1】:

如果使用 PDO,则需要像这样设置 utf8 编码:

$dbh = new PDO("mysql:host=127.0.0.1;dbname=recycle;charset=utf8",'root','');

【讨论】:

    【解决方案2】:

    在得到大家的大力帮助并检查重复链接后,解决方案是直接在mysqli_connect($host, $username, $password) 之后设置编码。

    这是通过添加mysqli_set_charset($connection, "utf8");来完成的

    我还将mysqli_fetch_array() 更改为mysqli_fetch_assoc(),这只是填充了关联数组,这不是解决方案,但非常有用。

    【讨论】:

      【解决方案3】:
       $table = $_GET["table"];
       $query = "SELECT * FROM invitrodb_v1.{$table}";
       $data = mysqli_query($conn, $query);
      

      切勿在您的代码中这样做。 从不。你可以在这里找到原因:http://en.wikipedia.org/wiki/SQL_injection

      【讨论】:

      • 谢谢我根据那篇文章和一些额外的文章修改了我的代码。我将继续致力于基本的注射预防工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      相关资源
      最近更新 更多