生成带有id路径的json

时间:2016-11-26 08:32:18

标签: php json

我有以下json响应,我使用php创建它。

[{"id":"1","title":"Clean Code","author":"Robert Martin","bookUrl":"http:\/\/amzn.to\/1DJybxH","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/clean_code.jpg","displayDate":"August 11, 2008","numberOfPages":"464"},{"id":"2","title":"Effective Java","author":"Joshua Bloch","bookUrl":"http:\/\/amzn.to\/1Ku8Xel","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/effective_java.jpg","displayDate":"May 28, 2008","numberOfPages":"346"},{"id":"3","title":"Working Effectively with Legacy Code","author":"Michael Feathers","bookUrl":"http:\/\/amzn.to\/1Jqe1PA","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/legacy_code.jpg","displayDate":"October 2, 2004","numberOfPages":"456"},{"id":"4","title":"Refactoring: Improving the Design of Existing Code","author":"Martin Fowler","bookUrl":"http:\/\/amzn.to\/1Lx4cjR","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/refactoring.jpg","displayDate":"July 8, 1999","numberOfPages":"464"}]

在这个响应中,我有一个查询,它将一本书作为json对象返回。

url link

因此,使用该URL,将返回id = 2的书。

{"id":"2","title":"Effective Java","author":"Joshua Bloch","bookUrl":"http:\/\/amzn.to\/1Ku8Xel","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/effective_java.jpg","displayDate":"May 28, 2008","numberOfPages":"346"}

但是,当我不在我的网址中使用id路径时,总会返回第一本书(id = 1)。我不确定这是否正确。

这是我的PHP代码。

<?php 
 include("init.php");
 $string="";
 $newString="";
 $id = mysqli_real_escape_string($con,$_GET['id']); 
 $get_posts = "select * from books"; 
 if($id != '') $get_posts .= " WHERE id LIKE '%{$id}%'"; 
 $run_book = mysqli_query($con,$get_posts);     

 $string =    
 json_encode(mysqli_fetch_object($run_book),JSON_UNESCAPED_UNICODE);

 echo $string; 

?>

有什么建议吗?

谢谢,

西奥。

0 个答案:

没有答案
相关问题