Reputation: 107
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: 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"
}
Upvotes: 1
Views: 2347
Reputation: 39243
If using PDO, you need to set the utf8 encoding like this:
$dbh = new PDO("mysql:host=127.0.0.1;dbname=recycle;charset=utf8",'root','');
Upvotes: 0
Reputation: 107
After much help from everyone and checking a duplicate link the solution was to set the encoding directly after mysqli_connect($host, $username, $password)
.
This was done by adding mysqli_set_charset($connection, "utf8");
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.
Upvotes: 3
Reputation: 1600
$table = $_GET["table"];
$query = "SELECT * FROM invitrodb_v1.{$table}";
$data = mysqli_query($conn, $query);
Never do that in your code. Never. You can find out why here: http://en.wikipedia.org/wiki/SQL_injection
Upvotes: 1