Home > Blockchain >  no data gets print where the query returns too many results
no data gets print where the query returns too many results

Time:10-11

i have a query which is suppose to retrieve data from mysql and print it on the browser it is working with a few rows of data but when the data gets bigger it just stops working and doesn't give any errors.

$result = mysql_query($query);


$json_array = array();
while($row = mysql_fetch_assoc($result)){
    $json_array[] = $row;
}

print mysql_error();
print json_encode($json_array);

this is my php info https://mxclogistics.com/phpinfo.php

i have tried every thing but nothing seems to work.

CodePudding user response:

I'm very surprised it works at all.

This would work if $row was formatted.

Use foreach()

I doubt this would work either:

foreach($json_array as $row){
  echo "$row<br>\n"
}

This should work (replace colx with the name of the table columns):

foreach($json_array as $row){
  echo $row['col1'] . ', ' . $row['col2'] . ', ' . $row['col2'] . "<br>\n"
}


foreach($json_array as $row){
  echo $row['name'] . ', ' . $row['address'] . ', ' . $row['phone'] . "<br>\n"
}

CodePudding user response:

@Lessmore has right, maybe the PHP process has reached the memory limits in the server, try with less columns or registers, but I understand your need, then you need to write your self json_encode code to write row by row. Some like this:

    $first = null;
    header('Content-Type: application/json');
    echo '[';
    while ($row = mysql_fetch_assoc($result)) {
        if ($first) {
            echo ',';
        }
        echo json_encode($row);
        !$first && $first = true;
    }
    echo ']';
    exit;
  • Related