【问题标题】:Problematic url query有问题的网址查询
【发布时间】:2017-04-06 01:08:12
【问题描述】:

我有这个 php 脚本,它给了我一个 json 响应。

 <?php 
    include("init.php");
    $string="";
    $newString="";
    $get_posts = "select * from books_table";       
    $run_posts = mysqli_query($con,$get_posts);     
    $posts_array = array(); 
    while ($posts_row = mysqli_fetch_array($run_posts)){
        $row_array['title'] = $posts_row['title'];
        $row_array['author'] = $posts_row['author'];
        $row_array['bookUrl'] = $posts_row['bookUrl'];
        $row_array['imageUrl'] = $posts_row['imageUrl'];
        $row_array['displayDate'] = $posts_row['displayDate'];
        $row_array['numberOfPages'] = $posts_row['numberOfPages'];
        array_push($posts_array,$row_array);            
     }     
       $string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);
       echo $string;?>

我得到的 json

[{"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"},{"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"},{"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"},{"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"}]

我想执行一个查询,该查询将返回标题包含单词 clean 的对象。

所以我正在使用这个网址

[http://www.theo-android.co.uk/books/sample_data.php/q=clean][1]

但是,我得到了和以前一样的 json 响应。不会过滤掉一个或多个对象。为什么会这样?

谢谢,

西奥。

【问题讨论】:

    标签: php json url


    【解决方案1】:

    如果我理解正确,您希望sample_data.php 能够返回过滤后的数据? 首先,您需要更新sample_data.php 来处理q 参数(我会将它用作GET,因为它更简单:http://www.theo-android.co.uk/books/sample_data.php?q=clean

    <?php 
    include("init.php");
    $string="";
    $newString="";
    $query = mysqli_real_escape_string($con,$_GET['q']); // get and escape the q param
    $get_posts = "select * from books_table"; 
    if($query != '') $get_posts .= " WHERE title LIKE '%{$query}%'"; // if $query is not empty string - query using a wild card
    $run_posts = mysqli_query($con,$get_posts);     
    $posts_array = array(); 
    while ($posts_row = mysqli_fetch_array($run_posts)){
        $row_array['title'] = $posts_row['title'];
        $row_array['author'] = $posts_row['author'];
        $row_array['bookUrl'] = $posts_row['bookUrl'];
        $row_array['imageUrl'] = $posts_row['imageUrl'];
        $row_array['displayDate'] = $posts_row['displayDate'];
        $row_array['numberOfPages'] = $posts_row['numberOfPages'];
        array_push($posts_array,$row_array);            
     }     
       $string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);
       echo $string;?>
    

    这样$posts_row就只有相关书籍了

    --添加----

    按 id 显示图书 json

    http://www.theo-android.co.uk/books/sample_data.php?id=1

    <?php 
    include("init.php");
    $string="";
    $newString="";
    $query = mysqli_real_escape_string($con,$_GET['q']); // get and escape the q param
    $id = (int)$_GET['id']; // get and cast to int the id var from GET
    $where_cond = array();
    $get_posts = "select * from books_table"; 
    if($query != '') $where_cond[] = " title LIKE '%{$query}%'"; // if $query is not empty string - query using a wild card
    if($id > 0) $where_cond[] = " id = {$id}"; // if $id is a number
    if(!empty($where_cond)) $get_posts .= " WHERE " . implode(" AND ",$where_cond);
    $run_posts = mysqli_query($con,$get_posts);     
    $posts_array = array(); 
    while ($posts_row = mysqli_fetch_array($run_posts)){
        $row_array['title'] = $posts_row['title'];
        $row_array['author'] = $posts_row['author'];
        $row_array['bookUrl'] = $posts_row['bookUrl'];
        $row_array['imageUrl'] = $posts_row['imageUrl'];
        $row_array['displayDate'] = $posts_row['displayDate'];
        $row_array['numberOfPages'] = $posts_row['numberOfPages'];
        array_push($posts_array,$row_array);            
     }     
       $string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);
       echo $string;?>
    

    因为您希望它同时适用于 id 和 q(以及其中一个),所以我将每个条件插入到数组中,然后使用 AND 分隔符将其内爆

    它未经测试。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2018-07-21
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多