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 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败