1

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"
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
sp_conway
  • 107
  • 11
  • Are you getting any errors? Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Dec 29 '14 at 20:31
  • @JayBlanchard I'm not getting any errors after adding that. The results are still the same. – sp_conway Dec 29 '14 at 20:41
  • 1
    `var_dump(json_encode($table_row));` – AbraCadaver Dec 29 '14 at 20:44
  • Part of what may be causing you some confusion is that you are using the more general `mysqli_fetch_array()` which populates both a numeric and associative key for each field into the response array. You probably want to do an associate-only fetch. Outside of that, there is no reason that `json_encode()` should not work. – Mike Brant Dec 29 '14 at 20:49
  • 1
    @AbraCadaver var_dump(json_encode($table_row)); is only displaying bool(false) on screen. Should I be calling var_dump inside my while loop like this: $table_row[] = var_dump($row); If I do this then every row is being displayed on screen with no limit. However if I only call var_dump after the array is filled I'm not getting anything. – sp_conway Dec 29 '14 at 21:09
  • @MikeBrant I changed my code to reflect your suggestion and I could still only display the limited number of rows but it was MUCH clearer thank you. – sp_conway Dec 29 '14 at 21:09
  • @sp_conway Did the number of rows shown change? Did you verify behavior in output source code or just in browser view? – Mike Brant Dec 29 '14 at 21:12
  • @MikeBrant number of rows did not change but I just updated what is being shown (with same limit of course). Verified in ouput source code. – sp_conway Dec 29 '14 at 21:16
  • if `json_encode` returns false, which you indicated in your previous comment, then it is failing to encode the array. Possible [duplicate](http://stackoverflow.com/questions/9098507/why-is-this-php-call-to-json-encode-silently-failing-inability-to-handle-singl) – Preston S Dec 29 '14 at 21:21
  • Show us the record from row 314. – Preston S Dec 29 '14 at 21:24
  • @PrestonS after checking that link I added mysqli_set_charset($conn, "utf8"); to my opendb.php script directly after the mysqli_connect line. This allows me to output every row without limit. Is setting the encoding something I was supposed to do when setting up the connection or is it something that could alter the ouput? – sp_conway Dec 29 '14 at 21:34

3 Answers3

3

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.

Rangad
  • 2,110
  • 23
  • 29
sp_conway
  • 107
  • 11
1
 $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

maxpovver
  • 1,580
  • 14
  • 25
  • Thanks I altered my code based on that read and some additional reading. I'll continue to work on basic injection prevention. – sp_conway Dec 29 '14 at 22:19
0

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','');
William Entriken
  • 37,208
  • 23
  • 149
  • 195