Jump to content

Recommended Posts

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:

 // 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.

 

Link to comment
https://forums.phpfreaks.com/topic/127123-search-multiple-sql-fields/
Share on other sites

Thanks,

I tried this

$query = "select * from BOL where * like '%$trimmed%' order by name";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

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 42

You have to specify a column to compare to $trimmed you can't just use *.  If you want to search multiple columns then you can use:

<?php
// 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";	

// found it all columns
$query = "select * from BOL where ".
		"column1 like '%$trimmed%' ".
		"and column2 like '%$trimmed%' ".
		"and column3 like '%$trimmed%' ".
		"and column4 like '%$trimmed%' ".
		"and column5 like '%$trimmed%' ".
		"and column6 like '%$trimmed%' ".
		"and column7 like '%$trimmed%' ".
		"and column8 like '%$trimmed%' ".
		"order by name";	
?>

DarkWater

Output was the same, it just worked.

What is it I should be learning here?

I'm fairly new to PHP I generally I prefer to hunt dow answeres on my own.

This one I could not find.

 

Okay, if you still had an error, it would have killed the script and shown you the MySQL error.  Since you fixed it, no error was shown (obviously).  Keep that piece of code handy for next time.  You should generally use that on every single mysql_query() call so you can catch MySQL errors.

Just make sure you disable them or write them to a log instead before you put the site into production since you want to keep the public viewable errors to a minimum.

 

Indeed.  You should always have some sort of error logging solution in effect on production sites.

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.