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

时间:2010-02-18 10:34:32

标签: php sql database

我有四个搜索字段,用于在数据库中搜索图书ID:s,然后回显结果。根据您选择从sql查询中搜索的字段,可以在下面的代码中看到。标题和isbn字段工作正常,但是当我尝试使用作者或类别字段时,没有返回任何内容。相关的数据库表也可以在下面看到。也许我使用sql函数LIKE ???

的方式有问题

数据库:

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代码:

<?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 />";
}
?>

使用author字段搜索时生成的查询:     选择DISTINCT bokId     来自博克     自然联合(选择*                   来自skrivenav                   自然加入forfattare                   在哪里fNamn LIKE('%Jonas%','%Alex%'))AS S

来自anthares的快速解决方案回答并且有效,所以谢谢!!

如果($ _ 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 个答案:

答案 0 :(得分:1)

我认为这段代码:

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 ";
只有当您按照写入的确切顺序作为真实作者的值子集传递时,

才会返回结果。因此,此查询不会检查加扰作者的姓名。

与类别相同。

您应该在过滤器中的每个类别或作者的where子句中添加“或”,并为每个类别或作者创建一个单独的表达式。