【发布时间】: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"
}
【问题讨论】:
-
您是否遇到任何错误?在您打开
<?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,我什么也得不到。