skip to Main Content

I have a query which is supposed 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);

I have tried everything but nothing seems to work.

2

Answers


  1. 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"
    }
    
    Login or Signup to reply.
  2. @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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search