猿问

使用 PHP 从 MySQL 查询中没有返回行,而手动运行查询返回行

我正在使用 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

)


慕娘9325324
浏览 105回答 2
2回答

MYYA

我认为以 0 开头的 user_id 造成了问题。您能否更改 user_id 列的数据类型并将其设为 varchar,然后尝试或从查询中删除 0 以及表行,然后进行检查。它应该工作。

元芳怎么了

if (isset($_POST["adds"])) {&nbsp; &nbsp; $servername = "localhost";&nbsp; &nbsp; $username = "root";&nbsp; &nbsp; $password = "root";&nbsp; &nbsp; $db = "loc";&nbsp; &nbsp; $conn = new mysqli($servername, $username, $password, $db);&nbsp; &nbsp; if ($conn->connect_error) {&nbsp; &nbsp; &nbsp; &nbsp; die("Connection failed: " . $conn->connect_error);&nbsp; &nbsp; }&nbsp; &nbsp; $response['is_error'] = 'no';&nbsp; &nbsp; $user = $_POST['user'];&nbsp; &nbsp; $date = $_POST['date'];&nbsp; &nbsp; $sql = "select * from location where user_id='$user' and DATE(timestamp) = '$date'";&nbsp; &nbsp; $locations = $conn->query($sql) or $response['is_error'] = 'yes';&nbsp; &nbsp; $response['num_rows'] = $locations->num_rows;&nbsp; &nbsp; $locations = $locations->fetch_assoc();&nbsp; &nbsp; $response['locations'] = $locations;&nbsp; &nbsp; $response['date'] = $date;&nbsp; &nbsp; if ($response['is_error'] == 'yes') {&nbsp; &nbsp; &nbsp; &nbsp; $response['status'] = "failed";&nbsp; &nbsp; &nbsp; &nbsp; $response['error'] = $conn->error;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $response['status'] = "ok";&nbsp; &nbsp; }&nbsp; &nbsp; $response['sql'] = $sql;&nbsp; &nbsp; echo json_encode($response);}`<html><head></head><body>&nbsp; &nbsp; <form method="post">&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="user"><br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="date"> <br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="adds">&nbsp; &nbsp; </form></body></html>
随时随地看视频慕课网APP
我要回答