| 
	
 | 
 Posted by Juliette on 11/10/05 19:08 
Ja NE wrote: 
> I'm trying to write a search script for my site. basic serch is fine, I 
> can search for some word in one or other or third table, column... no 
> problem, but now I would like to serch for name and family name of 
> rtegistered users. so I need to search in more than one table. for one 
> or two or even more words (imagine you are looking for Jan Michael 
> Bellay) do I need query with AND... o.k., here is what I have done so 
> far: 
>  
> $search = $_POST['serach'] 
> if(isset($search)) { 
>   // spliting search terms 
>   $oneword = explode(" ", $search); 
>   // counting numbers of terms to search 
>   $all=str_word_count($search); 
>   foreach($oneword as $number => $searchoneword) { 
>     // forming sql block if there is more than one term to search 
>     if($number < ($all-1)) { 
>       $sqlblock = "uname LIKE %$searchoneword% OR 
>                    name LIKE %$searchoneword% OR 
>                    famname LIKE %$searchoneword% AND "; 
>     } 
>     // forming sql block if there is one or is the last one to search 
>     else { 
>       $sqlblock = "uname LIKE %$searchoneword% OR 
>                    name LIKE %$searchoneword% OR 
>                    famname LIKE %$searchoneword% "; 
>     } 
>   } 
>   // and now... WHAT? 
> } 
>  
> I have tried all I know. and that isn't much. 
> tried to create array in foreach loop, couldnt get out what I wanted. 
> tried to write a function of above, to get out something with return() 
> but don't know what to do later... 
> I can echo what I want, but that isn't that... I need to put those block 
> together(!) in one variable which I will later use in slq query like: 
>  
> $query = "SELECT $alltogether ORDER BY id ACS"; 
>  
> anyone can suggest me what to do? 
> tnx 
>  
 
 
My two cents: 
* put the sql phrases between brackets so it will be clear which groups  
of    selection criteria belong together (you will get very strange  
results otherwise) 
* string the phrases together (take note of the .= in the below example) 
 
You would get something like: 
        $sqlblock .= "(uname LIKE %$searchoneword% OR 
                     name LIKE %$searchoneword% OR 
                     famname LIKE %$searchoneword%) AND "; 
 
 
And concerning the query... you need to use the WHERE clause. 
 
Good luck and have a look at the manuals. 
 
Grz, J.
 
[Back to original message] 
 |