---SOLVED---Search Multiple SQL Fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
chopper_pc
Forum Newbie
Posts: 15
Joined: Fri May 30, 2008 10:55 pm

---SOLVED---Search Multiple SQL Fields

Post by chopper_pc »

I have a simple search scrpt that is working searching one field in a SQL database that I want to use in another script to search multiple fields for the search term. This is the code used to specify the search field:

Code: Select all

// Build SQL Query  
$query = "select * from BOL where name like \"%$trimmed%\"  
  order by name"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
where "name" is what would be the syntax to add say a field called "name2" as well. I even tried a wild card but I received this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/results_bol.php on line 44
Any suggestions greatly appreciated.
Last edited by chopper_pc on Sun Oct 05, 2008 2:06 pm, edited 1 time in total.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Search Multiple SQL Fields

Post by pcoder »

Replace your query with:

Code: Select all

 
$query = "select * from BOL where name like '%".$trimmed."%'  
   order by name";
 
It works.. 8)
User avatar
chopper_pc
Forum Newbie
Posts: 15
Joined: Fri May 30, 2008 10:55 pm

Re: ---SOLVED---Search Multiple SQL Fields

Post by chopper_pc »

// all posibilities
$query = "select * from BOL where ".
"column1 like '%$trimmed%' ".
"or column2 like '%$trimmed%' ".
"or column3 like '%$trimmed%' ".
"or column4 like '%$trimmed%' ".
"or column5 like '%$trimmed%' ".
"or column6 like '%$trimmed%' ".
"or column7 like '%$trimmed%' ".
"or column8 like '%$trimmed%' ".
"order by name";
Works like a charm, just change "columnX" to your field names
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: ---SOLVED---Search Multiple SQL Fields

Post by VladSun »

Or use a MySQL FULL TEXT search ...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply