1

I am having trouble searching multiple columns in a MySQL database table with user input from a form.

Table in database is called vehicles and has 4 fields/columns: year, make, model, and option

The user inputs their search criteria from a form which I put into the $input variable.

Below is my code which doesn't work. It returns a boolean error:

(Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given)

$query = ("SELECT * FROM vehicles WHERE year LIKE '%$input%' OR make LIKE '%$input%' OR model LIKE '%$input%' OR option LIKE '%$input%'");

$result = mysql_query($query);

Yet, when I remove the final OR from the query, it works fine, but only searches the year, make, and model fields:

$query = ("SELECT * FROM vehicles WHERE year LIKE '%$input%' OR make LIKE '%$input%' OR model LIKE '%$input%'");

$result = mysql_query($query);

How do I get the query to search all 4 fields/columns? What am I missing?

Is there an easier way to have an entire table (all columns in the table) be searched for a users's input?

Really appreciate any help you can give me.

1 Answer 1

3

"option" is a reserved word in mysql, see here. So, your query actually fails and returns a boolean false instead of a resource. Rename the column or use `` around it like this

$query = ("SELECT * FROM vehicles WHERE year LIKE '%$input%' OR make LIKE '%$input%' OR model LIKE '%$input%' OR `option` LIKE '%$input%'");

Also be sure to use proper escaping to be safe against SQL Injection.

2
  • Thanks for the quick reply! I changed the column to EXTRA instead of OPTION and it works like a charm! I knew it was something simple. Thanks a million! Nov 18, 2012 at 16:35
  • You're welcome ;). Please don't forget to mark this as the solving answer. Nov 18, 2012 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.