echo响应php + json编码

时间:2014-04-03 14:40:14

标签: php mysql json

我的问题是,在下面的第一个代码中,它不会在浏览器上显示数据,只显示黑页。日志中没有错误。

另一件事是使用var_dump或print_r显示数据但是它没有以正确的json格式编码。

有没有回应回应的方法?在第二个代码中,我遵循与第一个完全相同的过程,它向我显示了我想要的数据,没有var_dum或print_r。

第一个代码

<?php
/*
 * Following code will list all the products
 */
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/conectaDB.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
// get all products from products table
$result = mysql_query("select cd.id, cd.dt_ini_camp, cd.dt_fim_camp, cd.descricao, (select sum(quantidade) from lin_doc where id_cab_doc = cd.id) as qtd from cab_doc cd where cd.id_tipo_doc = 1") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["cab_doc"] = array();
    while ($row = mysql_fetch_array($result)) {
        var_dump($row);
        // temp user array
        $linha                = array();
        $linha["id"]          = $row["id"];
        $linha["dt_ini_camp"] = $row["dt_ini_camp"];
        $linha["dt_fim_camp"] = $row["dt_fim_camp"];
        $linha["descricao"]   = $row["descricao"];
        $linha["qtd"]         = $row["qtd"];
        // push single product into final response array
        array_push($response["cab_doc"], $linha);
    }
    // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";
    // echo no users JSON
    echo json_encode($response);
}
?>

此代码的输出是空白页。

第二个代码

以下工作正常并显示数据:

<?php
/*
 * Following code will list all the products
 */
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/conectaDB.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_POST["id"])) {
    $id = $_POST['id'];
    // get all products from products table
    $result = mysql_query("SELECT id, id_estab, dt, hr, quantidade FROM lin_doc WHERE id_cab_doc = $id") or die(mysql_error());
    // check for empty result
    if (mysql_num_rows($result) > 0) {
        // looping through all results
        // products node
        $response["lin_doc"] = array();
        while ($row = mysql_fetch_array($result)) {
            //var_dump($row);
            // temp user array
            $linha               = array();
            $linha["id"]         = $row["id"];
            $linha["id_estab"]   = $row["id_estab"];
            $linha["dt"]         = $row["dt"];
            $linha["hr"]         = $row["hr"];
            $linha["quantidade"] = $row["quantidade"];
            // push single product into final response array
            array_push($response["lin_doc"], $linha);
        }
        // success
        $response["success"] = 1;
        // echoing JSON response
        echo json_encode($response);
    } else {
        // no products found
        $response["success"] = 0;
        $response["message"] = "No products found";
        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    // echoing JSON response
    echo json_encode($response);
}
?>

输出第二个代码:

  

{ “lin_doc”:[{ “ID”: “66”, “id_estab”: “12”, “DT”: “2013年3月26日”, “小时”: “16点30分〇〇秒”, “quantidade”: “373.000”},{ “ID”: “148”, “id_estab”: “16”, “DT”: “2013年7月8日”, “小时”: “20点45分00秒”, “quantidade”: “119.000”},{ “ID”: “260”, “id_estab”: “14”, “DT”: “2012年9月20日”, “小时”: “20点00分00秒”, “quantidade”: “324.000”},{ “ID”: “275”, “id_estab”: “33”, “DT”: “2012-07-23”, “hr” 是: “22点00分○○秒”, “quantidade”: “489.000”},{ “ID”: “306”, “id_estab”: “25”, “DT”: “2012-07-27”, “hr” 是: “二十时45分00秒”, “quantidade”: “338.000”},{ “ID”: “468”, “id_estab”: “15”, “DT”: “2013年4月23日”, “小时”: “20点45分00秒”, “quantidade”: “263.000”},{ “ID”: “546”, “id_estab”: “18”, “DT”: “2013年5月22日”, “小时”: “11时十五分00秒”, “quantidade”: “87.000”},{ “ID”: “578”, “id_estab”: “11”, “DT”: “2013年3月6日”, “小时”: “13时30分00秒”, “quantidade”: “30.000”},{ “ID”: “601”, “id_estab”: “07”, “DT”: “2013年2月23日”, “小时”: “二十时00分00秒”, “quantidade”: “87.000”},{ “ID”: “617”, “id_estab”: “14”, “DT”: “2012年11月23日”, “小时”: “20时45分○○秒”, “quantidade”: “234.000”},{ “ID”: “651”, “id_estab”: “03”, “DT”: “2013年5月12日”, “小时”: “二十时45分00秒”, “quantidade”: “311.000”},{ “ID”: “752”, “id_estab”: “06”, “DT”:“2013年1月1日3" , “hr” 是: “13时三十分00秒”, “quantidade”: “219.000”},{ “ID”: “803”, “id_estab”: “22”, “DT”:“2013-02- 23" , “hr” 是: “16时30分00秒”, “quantidade”: “396.000”},{ “ID”: “819”, “id_estab”: “06”, “DT”:“2012-10- 21" , “hr” 是: “18时15分〇〇秒”, “quantidade”: “333.000”},{ “ID”: “850”, “id_estab”: “03”, “DT”:“2013-01- 19" , “hr” 是: “14点三十○分00秒”, “quantidade”: “265.000”},{ “ID”: “899”, “id_estab”: “22”, “DT”:“2013-06- 15" , “hr” 是: “13:00:00”, “quantidade”: “453.000”},{ “ID”: “993”, “id_estab”: “10”, “DT”:“2013-02- 03" , “hr” 是: “19点30分00秒”, “quantidade”: “441.000”}],   “成功”:1}

1 个答案:

答案 0 :(得分:0)

在你的连接中:mysql_set_charset(&#39; utf8&#39;,$ link);

尝试:

<?php
$link = mysql_connect('db_host', 'db_user', 'db_password');
mysql_set_charset('utf8', $link);
$db_selected = mysql_select_db('your_db', $link);

或编码你的数组:

foreach( $row as $value ) {
    $value = utf8_encode( $value );
}

我不确定你的查询是否正常:

"as qtd from cab_doc cd where cd.id_tipo_doc = 1"
                    ^^^^