Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data from MYSQL into JSON using PHP

Tags:

json

php

mysql

I have the following quite simple test PHP code that extracts the data and puts it into JSON formatted text.

I get the following error..

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 1979603 bytes) in /var/www/test.php on line 33

Where line 33 is the json_encode() line.

Is there a way to make this more efficient? The PHP.ini is already set to 32M as max, hence sized up from the 8M standard!

 <?php
    require('../../admin/db_login.php');

    $db=mysql_connect($host, $username, $password) or die('Could not connect');
    mysql_select_db($db_name, $db) or die('');

    $result = mysql_query("SELECT * from listinfo") or die('Could not query');
    $json = array();

    if(mysql_num_rows($result)){
            $row=mysql_fetch_assoc($result);
        while($row=mysql_fetch_row($result)){
            //  cast results to specific data types

            $test_data[]=$row;
        }
        $json['testData']=$test_data;
    }

    mysql_close($db);

    echo json_encode($json);


    ?>
like image 374
Lee Armstrong Avatar asked Jan 27 '11 12:01

Lee Armstrong


People also ask

How do I export data from MySQL to JSON?

Exporting MySQL data to JSON using the CONCAT() and GROUP_CONCAT() functions. Using a combination of CONCAT() and GROUP_CONCAT() functions, data from SQL string can be converted into JSON format. More about the usage of INTO OUTFILE can be found in the How to export MySQL data to CSV article.

Can PHP send JSON?

Send JSON data via POST with PHP cURL Specify the URL ( $url ) where the JSON data to be sent. Initiate new cURL resource using curl_init(). Setup data in PHP array and encode into a JSON string using json_encode(). Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option.

What is JSON extract () function in MySQL?

We can use the JSON_EXTRACT function to extract data from a JSON field. The basic syntax is: JSON_EXTRACT(json_doc, path) For a JSON array, the path is specified with $[index] , where the index starts from 0: mysql> SELECT JSON_EXTRACT('[10, 20, 30, 40]', '$[0]'); +------------------------------------------+

What is $row in PHP?

Return Value: Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result set. PHP Version: 5+


1 Answers

You are probably encoding a very large dataset. You could encode each row, one row at a time instead of encoding it in one big operation.

<?php
require('../../admin/db_login.php');

$db=mysql_connect($host, $username, $password) or die('Could not connect');
mysql_select_db($db_name, $db) or die('');

$result = mysql_query("SELECT * from listinfo") or die('Could not query');

if(mysql_num_rows($result)){
    echo '{"testData":[';

    $first = true;
    $row=mysql_fetch_assoc($result);
    while($row=mysql_fetch_row($result)){
        //  cast results to specific data types

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        echo json_encode($row);
    }
    echo ']}';
} else {
    echo '[]';
}

mysql_close($db);

That way, each call to json_encode() only encodes a small array instead of a large one. The end result is the same. This is IMO the solution which will use the less memory.

like image 149
Andrew Moore Avatar answered Sep 22 '22 09:09

Andrew Moore