Search database using LIKE and wildcards
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 = e开发者_运维知识库xplode(",", $_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";
}
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.
精彩评论