0

我有个问题。我有一个将数据回显到 json 对象的 php 代码。这是我的代码:

<?php

$host = "localhost"; //Your database host server
$db = "..."; //Your database name
$user = "..."; //Your database user
$pass = "..."; //Your password

$connection = mysql_connect($host, $user, $pass);

//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");  
}
else
{
    //Attempt to select the database
    $dbconnect = mysql_select_db($db, $connection);

    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $query = "SELECT * FROM wedstrijden";
        $resultset = mysql_query($query, $connection);

        $records = array();
                    $response = array(); //extra            

        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records[] = $r;        
        }

        //Output the data as JSON
        echo json_encode($records);    

    }
} 
?>

这样做的结果是一个 .php 文件,它与 JSON 对象相呼应。但我想要一个显示 JSON 对象的 .json 文件(如 results.json 文件)。这可能吗?

你能帮助我吗?

提前致谢。

4

4 回答 4

2

添加 json 标头可能会有所帮助(在回显片段之前):

header("content-type:application/json");
于 2013-10-15T15:00:25.970 回答
1

创建/覆盖文件使用file_put_contents功能:

file_put_contents('url/to/your/file/records.json', json_encode($records), LOCK_EX);

从 php 读取(输出):

echo file_get_contents('url/to/your/file/records.json');

更新:

<?php
....
//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");  
}
else
{
    //Attempt to select the database
    $dbconnect = mysql_select_db($db, $connection);

    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $query = "SELECT * FROM wedstrijden";
        $resultset = mysql_query($query, $connection);

        $records = array();
        $response = array(); //extra            

        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records[] = $r;        
        }

        //Output the data as JSON
        $json = json_encode($records);    

        //NOTE: FOLDERS 'url' and 'file' SHOULD BE WRITABLE WITH PERMISSIONS - 777
        //IN CASE 'url' FOLDER PLACED IN SERVER'S ROOT
        //IF YOU'RE USING SOME FTP BROWSER CHANGE PERMISSIONS FOR 'url' 
        //FOLDER AND APPLY IT TO ALL ENCLOSED ITEMS
        file_put_contents('url/file/records.json', $json);

    }
} 
?>
于 2013-10-15T15:06:25.163 回答
0

file_put_contents 应该可以解决问题。

于 2013-10-15T15:00:09.113 回答
0

首先,让我们把MYSQL演讲移开。它已被弃用,不应再使用,因此请尝试MYSQLI其中一种PDO变体。

其次,听起来您想要一个大的 JSON 响应。如果是这种情况,请更改以下代码:

$query = "SELECT * FROM wedstrijden";
$resultset = mysql_query($query, $connection);
$records = array();
$response = array(); //extra            
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
    {
        $records[] = $r;        
    }
echo json_encode($records)

更像这样的东西:

$query = "SELECT * FROM wedstrijden";
$resultset = mysql_query($query, $connection);
$records = mysql_fetch_all($resultset);
echo json_encode($records, JSON_PRETTY_PRINT);
于 2013-10-15T15:05:49.900 回答