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);    

    }
} 
?>

この結果は、JSON オブジェクトをエコーする .php ファイルです。しかし、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

次に、1 つの大きな 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 に答える