Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch database using LIKE and wildcards
    primarykey
    data
    text
    <p>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???</p> <p><strong>Database:</strong></p> <pre><code>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`) ) </code></pre> <p><strong>PHP code:</strong></p> <pre><code>&lt;?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']!="" &amp;&amp; $_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]."&lt;br /&gt;"; } ?&gt; </code></pre> <p>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</p> <p><strong>Quick solution from anthares answer and it worked so thank you!!</strong></p> <p>if($_GET['search_author']!=""){</p> <pre><code>$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"; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload