Use PHP to dump mysql values into JSON file

前端 未结 3 586
醉酒成梦
醉酒成梦 2021-01-28 21:47

I am trying to generate a JSON file from a mysql database by using PHP. So far, I have:



        
3条回答
  •  长情又很酷
    2021-01-28 22:16

    Try something like this.

    error_reporting(-1);
    
    $result = mysql_query("SELECT * FROM wp_posts");
    
    $data = array();
    
    while ($row = mysql_fetch_array($result)) {
        $data['posts']['post_status'][] = $row['post_status'];
        $data['posts']['post_title'][] = $row['post_title'];
    }
    
    $json_string = json_encode($data);
    
    $file = 'file.json';
    file_put_contents($file, $json_string);
    

提交回复
热议问题