创建一个包含php执行结果的文件

时间:2012-11-19 10:56:51

标签: php android jackson

我有一个php文件选择表中的所有行,我想将结果内容存储在www /目录中的文件中,就像我可以解析该name.json文件并使用jackson库在Android中显示其内容。 这是我的php文件:

<?php header('Content-type: application/json');

/*
 * Following code will list all the surveys
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all surveys from surveys table
$result = mysql_query("SELECT * FROM surveys") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // surveys node
    $response["surveys"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $survey = array();
        $survey["id_survey"] = $row["id_survey"];
        $survey["question_survey"] = $row["question_survey"];
        $survey["answer_yes"] = $row["answer_yes"];
        $survey["answer_no"] = $row["answer_no"];




        // push single survey into final response array
        array_push($response["surveys"], $survey);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no surveys found
    $response["success"] = 0;
    $response["message"] = "No surveys found";

    // echo no users JSON
    echo json_encode($response);
}
?>

所以:php file =&gt;执行=&gt;将结果存储在file =&gt;中解析该文件并在Android中显示。

1 个答案:

答案 0 :(得分:2)

如果您想将JSON写入文件,只需添加:

file_put_contents( "filename.json", json_encode($response) );

旁边或代替echo;