dp6319 2010-02-18 10:34
浏览 34
已采纳

使用LIKE和通配符搜索数据库

I have four search fields that are used to search a database for book id:s and then echos out the result. Depending on wich field you choose to search from the sql query builds up as you can see in the code below. The title and isbn field are working fine but when I try to use the author or category field nothing gets returned. The relevent database tables can also be seen below. Maybe there´s something wrong with the way I use the sql function LIKE???

Database:

CREATE TABLE IF NOT EXISTS `bok` (
  `bokId` int(11) NOT NULL AUTO_INCREMENT,
  `bokTitel` varchar(100) DEFAULT NULL,
  `upplaga` varchar(100) DEFAULT NULL,
  `ISBN` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`bokId`)
)

CREATE TABLE IF NOT EXISTS `skrivenav` (
  `bokId` int(11) DEFAULT NULL,
  `fId` smallint(6) DEFAULT NULL
) 

CREATE TABLE IF NOT EXISTS `forfattare` (
  `fId` smallint(6) NOT NULL,
  `fNamn` varchar(80) DEFAULT NULL,
  PRIMARY KEY (`fId`)
) 

CREATE TABLE IF NOT EXISTS `bokkat` (
  `bokId` int(11) DEFAULT NULL,
  `katId` smallint(6) DEFAULT NULL
) 

CREATE TABLE IF NOT EXISTS `kategori` (
  `katId` smallint(6) NOT NULL,
  `katNamn` varchar(80) DEFAULT NULL,
  PRIMARY KEY (`katId`)
) 

PHP code:

<?php    
$q = "SELECT DISTINCT bokId FROM ";
if($_GET['search_title']!=""||$_GET['search_ISBN']!=""){
    $q = $q."(SELECT * FROM bok WHERE ";
    if($_GET['search_title']!="")   
        $q = $q."bokTitel LIKE '%$_GET[search_title]%' ";
    if($_GET['search_title']!="" && $_GET['search_ISBN']!="")
        $q = $q."AND ";
    if($_GET['search_ISBN']!="")    
        $q = $q."ISBN LIKE '%$_GET[search_ISBN]%' ";
    $q = $q.") AS F";
}
else $q = $q."bok";                         
if($_GET['search_author']!=""){
    $author = explode(",", $_GET['search_author']);
    $auth = "";
    foreach ($author as $value){
        $auth = $auth . "%" . $value . "%', '";
    }
    $auth = trim($auth, ", '"); 
    $q = $q." NATURAL JOIN (SELECT * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ('$auth')) AS S ";
}
if($_GET['search_category']!="") {
    $category = explode(",", $_GET['search_category']);
    $cat = "'";
    foreach ($category as $value){
        $cat = $cat . "%" . $value . "%', '";
    }
    $cat = trim($cat, ", '");
    $q = $q." NATURAL JOIN (SELECT * FROM bokkat NATURAL JOIN kategori WHERE katNamn LIKE ('$cat')) AS K ";
}
$rs = mysql_query($q);
confirm_query($rs);    
while($row = mysql_fetch_row($rs)){
    echo $row[0]."<br />";
}
?>

Generated query when searching with author field: SELECT DISTINCT bokId FROM bok NATURAL JOIN (SELECT * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ('%Jonas%', '%Alex%')) AS S

Quick solution from anthares answer and it worked so thank you!!

if($_GET['search_author']!=""){

$author = explode(",", $_GET['search_author']);

$auth = "";

$q = $q.    " NATURAL JOIN (SELECT * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ";

foreach ($author as $value){

    $auth = $auth . "%" . $value . "%'";

    $q = $q.    "'$auth OR ";

    $auth = "";

}

$q = trim($q, " OR");

$q = $q.    ") AS A";

}

  • 写回答

1条回答 默认 最新

  • douzhizao0270 2010-02-18 10:51
    关注

    I think this piece of code:

    foreach ($author as $value){
    
        $auth = $auth . "%" . $value . "%', '";
    
    }
    $auth = trim($auth, ", '"); 
    
    $q = $q." NATURAL JOIN (SELECT * FROM skrivenav NATURAL JOIN forfattare WHERE fNamn LIKE ('$auth')) AS S ";
    

    will return a result only if you pass as value subset of the real authors in exact order which is written. So this query doesn't check for scrambled authors' names.

    The same thing with categories.

    You should add an "or" in your where clause for every category or author in your filter and make a separate expression for each of them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应