简体   繁体   English

如何使用PHP +大数据库将数据从MySQL转换为JSON

[英]How to Convert Data from MySQL to JSON using PHP + big database

I am building on from this code here where how to Convert Data from MySQL to JSON using PHP is shown. 我在此处的此代码的基础上,展示了如何使用PHP将数据从MySQL转换为JSON。 I have got this to work for a subset of my database(39,000 rows) but not the whole database(it basically just hangs getting 500 Internal Server Error in the Net tab in firebug and nothing is written in the files). 我已经将它用于我的数据库的一部分(39,000行),但不适用于整个数据库(它基本上只是在Firebug的“ Net”选项卡中挂起500 Internal Server Error,并且文件中没有任何内容)。 The code basically writes the DB to 2 files to a json file and a js file with var = <json array> 该代码基本上将数据库写入两个文件,分别为var = <json array>的json文件和js文件。

here is a snipet of what works: 以下是行之有效的摘要:

//$sql = "select * from table1; //does not work
//$sql = "select * from table1 LIMIT 0,10"; //for test
$sql = "select * from table1 LIMIT 0,39000"; //works 39000ish is the max

Is there any way I can alter my code to handle this bigger DB of 300,000+ rows and counting? 有什么办法可以更改我的代码,以处理更大的300,000+行和递增的DB?

Here is my complete code: 这是我完整的代码:

  <?php
    /* Database connection start */
    $servername = "localhost";
    $username = "root";
    $password = "Password1";
    $dbname = "test";

        //open connection to mysql db
        $connection = mysqli_connect($servername, $username, $password, $dbname) or die("Error " . mysqli_error($connection));

        //fetch table rows from mysql db
        //$sql = "select * from table1; //does not work
        //$sql = "select * from table1 LIMIT 0,10"; //for test
        $sql = "select * from table1 LIMIT 0,39000"; //works 39000ish is the max
        $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

        //create an array
        $emparray2 = array();
        while($row =mysqli_fetch_assoc($result))
        {
            $emparray2[] = $row;
        }
        echo json_encode($emparray2);

        //close the db connection
        mysqli_close($connection);

        //write to json file
        $fp = fopen('empdata2.json', 'w');
        fwrite($fp, json_encode($emparray2));
        fclose($fp);

        //write to js file with var data = [ <ARRAY>]
        $fp = fopen('data2.js', 'w');
        fwrite($fp, "var data2 =");
        fwrite($fp, json_encode($emparray2, JSON_PRETTY_PRINT));//makes it pretty
        //fwrite($fp, json_encode($emparray2));
        fclose($fp);

    ?>

possible similar questions here: 这里可能存在类似的问题:

1 here 2 here 3 here 4 here 5 here 1 这里 2 这里 3 这里 4 这里 5 这里

Edit1 编辑1

For my reference this is that I have to change. 供我参考,这是我必须进行更改。

root@a4b8b0a15197:/# cat /etc/php5/apache2/php.ini | grep -n memory_limit
406:memory_limit = 128M
root@a4b8b0a15197:/#

You can use this function. 您可以使用此功能。

<?php
set_time_limit ( 0 ) // no limit execution time.

/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "Password1";
$dbname = "test";

And, you have to increase memory limit like THIS 而且,你必须增加内存限制像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM