Bman900 Posted April 16, 2010 Share Posted April 16, 2010 So here is the code in question if (count($error) < 1) { $searchSQL = "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE "; // grab the search types. $types = array(); $types[] = isset($_GET['body'])?"`sbody` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['title'])?"`stitle` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['desc'])?"`sdescription` LIKE '%{$searchTermDB}%'":''; $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "`sbody` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked $andOr = isset($_GET['matchall'])?'AND':'OR'; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `stitle`"; // order by title. $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); Now I want to make it so it searches throug the tilte and article of a blog no matter what. Do you guys think this would work? Am I correct using AND or should I be using OR? if (count($error) < 1) { $searchSQL = "SELECT id, title, content FROM blog WHERE 'title' LIKE $searchtermDB AND WHERE 'content' LIKE $searchtermDB"; // order by title. $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); Quote Link to comment https://forums.phpfreaks.com/topic/198802-php-search-from-tutorial-on-this-site/ Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 Your query will only work with a sine 'phrase' or 'word' (including spaces). So i would use OR as i doubt you will get much results with an AND. And i would query the OR like this: SELECT id, title, content FROM blog WHERE title LIKE '%$searchtermDB%' OR content LIKE '%$searchtermDB%' Your SQL: You only need one WHERE clause. Only VALUES should be encapsulated with a single quote. NOTE: If you want multiple words you need to use loops etc, i would suggest looking up some tutorials before attempting that. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198802-php-search-from-tutorial-on-this-site/#findComment-1043416 Share on other sites More sharing options...
Bman900 Posted April 16, 2010 Author Share Posted April 16, 2010 Ok thanks, and I just searched for some tutorials on doing phrases but only one term searches such as this came up. Does any one know of a tutorial out there that explains how to search a pharse through the database? Quote Link to comment https://forums.phpfreaks.com/topic/198802-php-search-from-tutorial-on-this-site/#findComment-1043429 Share on other sites More sharing options...
ChemicalBliss Posted April 17, 2010 Share Posted April 17, 2010 The way i would do it (easy/lazy), is make a query something like: SELECT * FROM table WHERE (`somecol1` LIKE '%search1%' OR `somecol2` LIKE '%search1%' OR `somecol2` LIKE '%search1%' OR `somecol1` LIKE '%search1%') So you just do all the possible variations, you can do this dynamically with arrays and loops. ----- $columns = array('title','desc','author'); $phrase = "Some Search Phrase"; $words = explode(" ",$phrase); $sql = "SELECT * FROM table WHERE "; $terms = array(); foreach($columns As $col){ foreach($words As $word){ $terms[] = "`".$col."` LIKE '%".$word."%'"; } } $sql .= implode(" OR ",$terms); $result = mysql_query($sql) or die(mysql_error()); echo($sql); // see how it works. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/198802-php-search-from-tutorial-on-this-site/#findComment-1043438 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.