我有一个db与"id,用户id,时间戳,消息,地址,纬度,经度",我有一个文件"get_thoughts.php",显示我在一个表的db表的内容。但我有另一个文件"get_thinkts_json .php",只生成一个json发送到android应用程序。问题是,生成器是空的,当我调用它,没有什么里面,正如我所说的,与第一个文件,我得到所有的数据。
有什么问题吗?
<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);
}
?>
文件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);
}
?>
谢谢你的建议。
一些错误:<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 />
这是错误的,执行两次echo将使json无效:
header('Content-type: application/json');
echo json_encode(array('thoughts'=>$response));
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
你可能想要这样写:
header('Content-type: application/json');
$response['thoughts'] = $response;
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
在出现白屏的情况下,您还应该启用错误处理和/或检查错误日志中的消息。
你应该切换到PDO或mysqli作为mysql_*
函数被弃用,这可能会产生警告(输出…),这将导致你的header()
调用失败
maybe move
header("Content-type: application/json");
放到文件的顶部