我正在使用 jquery ajax 调用返回 JSON 的 PHP 页面简单 API。这是代码
<?php
$response['is_error'] = 'no';
$user = $_POST['user'];
$date = $_POST['date'];
$sql = "select * from locations where user_id='$user' and DATE(timestamp) = '$date'";
$locations = $conn->query($sql) or $response['is_error'] = 'yes';
$response['num_rows'] = $locations->num_rows;
$locations = $locations->fetch_assoc();
$response['locations'] = $locations;
$response['date'] = $date;
if($response['is_error'] == 'yes'){
$response['status'] = "failed";
$response['error'] = $conn->error;
}else{
$response['status'] = "ok";
}
$response['sql']=$sql;
echo json_encode($response);
执行此脚本时返回以下 JSON
{"is_error":"no","num_rows":0,"locations":null,"date":"2019-07-30","status":"ok","sql":"select * from locations where user_id='0123456789' and DATE(timestamp) = '2019-07-30'"}
以下是解析后的版本,方便观看
date: "2019-07-30"
is_error: "no"
locations: null
num_rows: 0
sql: "select * from locations where user_id='0123456789' and DATE(timestamp) = '2019-07-30'"
status: "ok"
但是,如果我复制粘贴在 PHPMyAdmin 页面 SQL 中以 JSON 回显的 SQL,它会返回一行,这确实是有意为之,奇怪的是,在脚本中使用 PHP 执行相同的查询时,它不返回任何内容。
这是 PHPMyAdmin 查询输出的附加屏幕截图
print_r($conn)在输出之后运行回声
mysqli Object
(
[affected_rows] => 1
[client_info] => 5.6.30
[client_version] => 50630
[connect_errno] => 0
[connect_error] =>
[errno] => 0
[error] =>
[error_list] => Array
(
)
[field_count] => 1
[host_info] => Localhost via UNIX socket
[info] =>
[insert_id] => 0
[server_info] => 5.6.44-cll-lve
[server_version] => 50644
[stat] => Uptime: 94095 Threads: 23 Questions: 77085244 Slow queries: 625 Opens: 814448 Flush tables: 1 Open tables: 5000 Queries per second avg: 819.227
[sqlstate] => 00000
[protocol_version] => 10
[thread_id] => 1035427
[warning_count] => 0
)
MYYA
元芳怎么了