首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >print_r显示数组内容,但未显示回送json_encode

问print_r显示数组内容,但未显示回送json_encode
EN

Stack Overflow用户
提问于 2014-12-29 20:28:48
回答 3查看 2.2K关注 0票数 1

我正在尝试json_encode以下mysqli结果。我的问题是在将每一行添加到我的数组后,我无法“回显json_encode”我的数组,但我能够print_r该数组。如果我为这个特定的查询添加了一个限制,那么echo json_encode可以工作,但是这个限制是313。我认为这表明了一个php内存限制问题,但是在将我的memory_limit更改为256 m之后,我仍然只能返回相同数量的数据。我尝试过寻找类似的问题,但没有什么比我面临的问题更接近的了。下面粘贴了我的整个php脚本。如有任何建议,敬请见谅。

代码语言:javascript
复制
<?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行类似于this..fyi,这是从这里找到的公共数据库中的数据,http://www.epa.gov/ncct/toxcast/data.html (以防有人好奇)。

代码语言:javascript
复制
{
    "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"
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-29 21:44:11

在每个人提供大量帮助并检查重复链接之后,解决方案是在mysqli_connect($host, $username, $password)之后直接设置编码。

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

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

票数 3
EN

Stack Overflow用户

发布于 2014-12-29 22:02:29

代码语言:javascript
复制
 $table = $_GET["table"];
 $query = "SELECT * FROM invitrodb_v1.{$table}";
 $data = mysqli_query($conn, $query);

从不在您的代码中使用。从不。你可以在这里找出原因:injection

票数 1
EN

Stack Overflow用户

发布于 2015-11-24 18:32:56

如果使用PDO,则需要设置如下utf8编码:

代码语言:javascript
复制
$dbh = new PDO("mysql:host=127.0.0.1;dbname=recycle;charset=utf8",'root','');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27695549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档