我有一个问题让我一整天都在头脑中...
我试图检索我的JSON对象,但它无法检索数据。
我正在发出GET请求。
如果'id'是空的,它应该检索我的数据库(PHPMyAdmin)的数组列表中的所有笔记。有任何想法吗?
我得到的错误是这样的:
PHP VERSION: 5.6.21 Connected Successfully
Notice: Trying to get property of non-object in
C:\xampp\htdocs\notes.phpon line
165
{
"header": {
"msg": "You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near ''notes' WHERE id=6'
at line 1",
"code": 400
},
"body": []
}这是代码
else if ($method === 'GET')
{
$sql = "";
if(empty($_REQUEST['id']))
{
// GET All Notes
$sql = "SELECT * FROM 'notes' ORDER BY created_date DESC";
}
else
{
//Get one Note
$id = $_REQUEST['id'];
$sql = "SELECT * FROM 'notes' WHERE id=$id";
}
$result = $conn->query($sql);
if($result->num_rows > 0) //LINE 165
{
$body = array();
//output data for each row
while($row = $result->fetch_assoc())
{
array_push($body, $row);
}
$json =
[
'header' =>
[
'msg' => "OK - Everything is working",
'code' => 200
],
'body' => $body
];
echo json_encode($json, JSON_PRETTY_PRINT);
}
else
{
$json =
[
'header' =>
[
'msg' => $conn->error,
'code' => 400
],
'body' => []
];
echo json_encode($json, JSON_PRETTY_PRINT);
}
$conn->close();
}
?>