print_r显示数组内容,但未显示echo json_encode [英] print_r showing array contents but echo json_encode is not

查看:129
本文介绍了print_r显示数组内容,但未显示echo json_encode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to json_encode the following mysqli results. My issue is after adding each row to my array I am unable to 'echo json_encode' my array but I am able to print_r the array. If I add a limit to this particular query then echo json_encode works but that limit is 313. I thought that indicated a php memory limit issue but after changing my memory_limit to 256M I am still only able to return the same amount of data. I've tried searching for similar issues but nothing was close enough to the issue I'm facing. My entire php script is pasted below. Any advice is appreciated.

<?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***//
?>

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

edit: This is an example of what I am returning. For this specific table there are 342 rows that are similar to this..fyi this is data from a public database found here http://www.epa.gov/ncct/toxcast/data.html (in case anyone is curious).

{
    "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"
}

推荐答案

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

After much help from everyone and checking a duplicate link the solution was to set the encoding directly after mysqli_connect($host, $username, $password).

这是通过添加mysqli_set_charset($connection, "utf8");

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

I also changed mysqli_fetch_array() to mysqli_fetch_assoc() which populated just the associative array which was not the solution but was incredibly helpful.

这篇关于print_r显示数组内容,但未显示echo json_encode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆