Jump to content

Recommended Posts

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}");      

Link to comment
https://forums.phpfreaks.com/topic/198802-php-search-from-tutorial-on-this-site/
Share on other sites

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-

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-

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.