dtd793353 2014-05-20 21:16
浏览 23

生成JSON,我获得了一个空白屏幕[重复]

This question already has an answer here:

I have a db with "id, userid, timestamp, message, address, latitude, longitude" and I have a file "get_thoughts.php" that shows me in a table the content of the table of the db. But I have another file "get_thoughts_json.php" that only generates me a json to be sended to an android app. The problem is that the generator is empty when I call it, with nothing inside, as I said, with the first file I get all the data.

What is the problem?

 <style type="text/css">
.datagrid table { border-collapse: collapse; text-align: left; width: 100%; } .datagrid {font: normal 12px/150% Arial, Helvetica, sans-serif; background: #fff; overflow: hidden; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; }.datagrid table td, .datagrid table th { padding: 5px 10px; }.datagrid table thead th {background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #006699), color-stop(1, #00557F) );background:-moz-linear-gradient( center top, #006699 5%, #00557F 100% );filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#006699', endColorstr='#00557F');background-color:#006699; color:#FFFFFF; font-size: 12px; font-weight: bold; } .datagrid table thead th:first-child { border: none; }.datagrid table tbody td { color: #00557F; font-size: 12px;font-weight: normal; }.datagrid table tbody .alt td { background: #E1EEf4; color: #00557F; }.datagrid table tbody td:first-child { border-left: none; }.datagrid table tbody tr:last-child td { border-bottom: none; }.datagrid table tfoot td div { border-top: 1px solid #006699;background: #E1EEf4;} .datagrid table tfoot td { padding: 0; font-size: 12px } .datagrid table tfoot td div{ padding: 3px; }.datagrid table tfoot td ul { margin: 0; padding:0; list-style: none; text-align: right; }.datagrid table tfoot  li { display: inline; }.datagrid table tfoot li a { text-decoration: none; display: inline-block;  padding: 2px 8px; margin: 1px;color: #FFFFFF;border: 1px solid #006699;-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #006699), color-stop(1, #00557F) );background:-moz-linear-gradient( center top, #006699 5%, #00557F 100% );filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#006699', endColorstr='#00557F');background-color:#006699; }.datagrid table tfoot ul.active, .datagrid table tfoot ul a:hover { text-decoration: none;border-color: #00557F; color: #FFFFFF; background: none; background-color:#006699;}div.dhtmlx_window_active, div.dhx_modal_cover_dv { position: fixed !important; }
</style>

<?php

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

// 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 products from products table
$result = mysql_query("SELECT * FROM thoughts") or die(mysql_error());

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

    echo '<center><div class="datagrid"><table>';
    echo '<thead><tr><th>ID</th><th>USERID</th><th>TIMESTAMP</th><th>MESSAGE</th><th>ADDRESS</th><th>LATITUDE</th><th>LONGITUDE</th></tr></thead><tbody>';

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $thought = array();
        $thought["id"] = $row["id"];
        $thought["userid"] = $row["userid"];
        $thought["timestamp"] = $row["timestamp"];
        $thought["message"] = $row["message"];
        $thought["address"] = $row["address"];
        $thought["latitude"] = $row['latitude'];
        $thought["longitude"] = $row['longitude'];


        echo '<tr><td>'.$row['id'].'</td><td>'.$row['userid'].'</td><td>'.$row['timestamp'].'</td><td>'.$row['message'].'</td><td>'.$row['address'].'</td><td>'.$row['latitude'].'</td><td>'.$row['longitude'].'</td></tr>';
        //echo '<tr><td class="tg-center">'.$row['id'].'</td><td class="tg-center">'.$row['username'].'</td><tdclass="tg-center">'.$row['timestamp'].'</td><td class="tg-center">'.$row['classroom'].'</td></tr>';
        // push single product into final response array
        array_push($response["thoughts"], $thought);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
    echo '</table></center>';
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No events found";

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

?>

File get_thoughts_json.php

<?php

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

// 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 products from products table
$result = mysql_query("SELECT * FROM thoughts") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {

    while($row = mysql_fetch_assoc($result)) {
      $response[] = array('thought'=>$row);
    }

    header('Content-type: application/json');
    echo json_encode(array('thoughts'=>$response));

    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);

} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No events found";

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

?>

Thanks for your advices.

Some errors:

<br />
<b>Deprecated</b>:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>C:\xampp\htdocs\iw\thoughts\db_connect.php</b> on line <b>30</b><br />
</div>
  • 写回答

2条回答 默认 最新

  • dowjgrm6787 2014-05-20 21:20
    关注

    maybe move

    header("Content-type: application/json");
    

    to the top of the file

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算