简体   繁体   English

PHP AND子句未显示结果,但OR为

[英]PHP AND clause not showing results but OR is

I have a book search that is searching for books i am looking to have it search for books that share authors categories or publishers. 我有一个图书搜索,它正在搜索要搜索共享作者类别或出版商的图书的图书。 I have it set up and it works for the OR clause eg books that have a category CHILDRENS or HISTORY related to them this works correctly but when i search for books that belong to 2 categories (AND) eg CHILDRENS AND MAGIC (Harry Potter) it does not show these book even though they are linked in the database. 我已经设置好它,并且适用于OR子句,例如,具有与之相关的类别CHILDRENS或HISTORY的书籍,这可以正常工作,但是当我搜索属于2类(AND)的书籍,例如CHILDRENS AND MAGIC(Harry Potter)时,即使它们在数据库中链接,也不会显示这些书。 在此处输入图片说明

Above is the search i have done using OR, When i do a search for books that belong to Childrens AND HISTORY 以上是我使用“或”进行的搜索,当我搜索属于“儿童与历史”的图书时 在此处输入图片说明

在此处输入图片说明

Above you can see i get no results for books that share Childrens and Magic when Harrypotter books do belong to both of these. 在上方,您可以看到当哈利波特的书确实属于这两个书时,共享《儿童与魔术》的书没有任何结果。 Above is the link in the database that gives every book their category, Magic is Category 1 and Childrens is Category 2 and you can see they both share these. 上面是数据库中的链接,该链接为每本书分配了类别,Magic是类别1,Childrens是类别2,您可以看到他们都共享这些类别。

Below is the PHP code for the Query's 以下是查询的PHP代码

<?php
include 'header.php';
include 'searchscriptTEST.php';

$sql =  "SELECT DISTINCT bk.title AS Title, bk.bookid AS BookID, bk.year AS Year, bk.publisher AS Publisher, aut.authorname AS Author 
         FROM book bk 

         JOIN book_category bk_cat 
         ON bk_cat.book_id = bk.bookid

         JOIN categories cat 
         ON cat.id = bk_cat.category_id

         JOIN books_authors bk_aut 
         ON bk_aut.book_id = bk.bookid

         JOIN authors aut
         ON aut.id = bk_aut.author_id";

if(isset($_GET['searchInput'])){
$input = $_GET['searchInput'];
$input = preg_replace('/[^A-Za-z0-9]/', '', $input);
}
if (isset($input)){

    $getters = array();
    $queries = array();

    foreach ($_GET as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if (!empty($temp)){
        if (!in_array($key, $getters)){
            $getters[$key] = $value;
            }
        }
    }

    if (!empty($getters)) {

        foreach($getters as $key => $value){
            //${$key} = $value;
            switch ($key) {
                case 'searchInput':
                    array_push($queries,"(bk.title LIKE '%{$getters['searchInput']}%' 
                    || bk.description LIKE '%{$getters['searchInput']}%' || bk.isbn LIKE '%{$getters['searchInput']}%' 
                    || bk.keywords LIKE '%{$getters['searchInput']}%' || aut.authorname LIKE '%{$getters['searchInput']}%')");
                break;
                case 'srch_publisher':
                    array_push($queries, "(bk.publisher = '{$getters["srch_publisher"]}')");
                break;
                case 'Year':
                    if(isset($getters['Year1']) ==""){
                        array_push($queries, "(bk.year = '{$getters['Year']}')");
                    } else {
                        array_push($queries, "(bk.year BETWEEN '$value' AND '{$getters['Year1']}')");
                    }
                break;
                case 'srch_author':
                     if(isset($getters['authorOperator']) ==""){
                    array_push($queries, "(bk_aut.author_id = '{$getters["srch_author"]}')");
                    } else {
                        $operator = $getters['authorOperator'];
                        array_push($queries, "(bk_aut.author_id = '$value' $operator bk_aut.author_id = '{$getters['srch_author1']}')");
                    }
                break;
                case 'srch_category':
                     if(isset($getters['catOperator']) ==""){
                        array_push($queries, "(bk_cat.category_id = '{$getters["srch_category"]}')");
                    } else {
                        $operator1 = $getters['catOperator'];
                        array_push($queries, "(bk_cat.category_id = '$value' $operator1 bk_cat.category_id = '{$getters['srch_category1']}')");
                    }
                break;

        }
    }
}
if(!empty($queries)){
    $sql .= " WHERE ";
    $i = 1;
    foreach ($queries as $query) {
        if($i < count($queries)){
            $sql .= $query." AND ";
        } else {
            $sql .= $query;
        }   
        $i++;
    }
}
$sql .= " GROUP BY bk.title ORDER BY bk.title ASC";
var_dump($sql);

}else{
    $sql .= " GROUP BY bk.title ORDER BY bk.title ASC";
}


$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
?>

This is the SQL Dump that gets sent to the database, 这是发送到数据库的SQL Dump,

SELECT
  DISTINCT bk.title AS Title,
  bk.bookid AS BookID,
  bk.year AS Year,
  bk.publisher AS Publisher,
  aut.authorname AS Author 
FROM book bk
  JOIN book_category bk_cat ON bk_cat.book_id = bk.bookid
  JOIN categories cat ON cat.id = bk_cat.category_id
  JOIN books_authors bk_aut ON bk_aut.book_id = bk.bookid
  JOIN authors aut ON aut.id = bk_aut.author_id 
WHERE
  (bk_cat.category_id = '2' AND bk_cat.category_id = '1')
GROUP BY bk.title
ORDER BY bk.title ASC

After formatting your query so that it's actually readable you see the line: 格式化查询使其实际可读之后,您会看到以下行:

bk_cat.category_id = '2' AND bk_cat.category_id = '1'

That wont work, category_id can not be both 2 and 1 at the same time. 那行不通, category_id不能同时为21

Your query should look something like: 您的查询应类似于:

SELECT books.*
FROM books
  JOIN book_categories bc1 ON books.id = bc1.book_id AND bc1.category_id = 1
  JOIN book_categories bc2 ON books.id = bc2.book_id AND bc1.category_id = 2

You need to JOIN twice on categories, or as many times as you have categories to match. 您需要在类别上JOIN 两次 ,或在要匹配的类别上进行多次。

There are multiple ways to do this but the simplist would be to use a WHERE clause something like this to SELECT books which are in more then one category: 有多种方法可以做到这一点,但简化列表将是使用WHERE子句来SELECT书籍,这些书籍不止一个类别:

WHERE
        `bk`.`book_id` IN (SELECT `book_id` FROM `book_category` WHERE `category_id` = '2')
    AND `bk`.`book_id` IN (SELECT `book_id` FROM `book_category` WHERE `category_id` = '1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM