user3036014 user3036014 - 2 years ago 66
SQL Question

Search Exercise error "Unknown column in where clause"

I've been coding a search exercise when I stumbled upon this error

Unknown column in where clause

As I type a number to search for the cottage number, it returns without fail, but if I type a customer name the error shows.

This is my SQL query:

$raw_results = mysql_query("SELECT * FROM pastrec
WHERE (`cott_num` LIKE $query) OR (`cus_name` LIKE '%".$query."%')") or die(mysql_error());

I know that I have a problem in my query but I'm not sure what.


I've omitted the
as search parameter and it worked! You can search names but you can't search numbers. I've reversed the technique above and omitted the
and it also works! you can search cottage numbers but you can't search names.

I really want to have a search that search both cottage number or customer name.

Answer Source

you are trying to compare number or string in this query. When you are comparing number (id) you should use equal. you need to use quotation because if you input a string then it will trying to comapre like this: cott_num = your_string this is cause error in mysql.

$sql = "SELECT * FROM pastrec
                WHERE (`cott_num` = '$query') OR (`cus_name` LIKE '%".$query."%')";
echo $sql;

  $raw_results = mysql_query($sql) or die(mysql_error());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download