MySQL Select Statement Problem



sallie_ann said:
I am being given the following error message;

SELECT name, County, sector, company_size FROM beacon WHERE company_size BETWEEN 1 AND 9 AND Problem

First, a tip: When you're writing SQL queries, open up the SQL client prompt (mysql.exe) and write test queries there before turning them into query strings in PHP. The MySQL client will give you better feedback, and you'll know if your queries work before putting them into PHP. Trying to write SQL queries from scratch in PHP is like riding a bicycle with your eyes closed.

Anyway, the answer to your question is that in your query, you haven't enclosed your strings in quotation marks. MySQL doesn't know what a Derbyshire (without quotes) is, but it knows that 'Derbyshire' (with quotes) is a string.

So, instead of this:

if($_POST['County'] == 1) 
$where1 = 'County LIKE Derbyshire';

You're better off with this:

if($_POST['County'] == 1) 
$where1 = 'County LIKE \'Derbyshire\'';

Though I'm also compelled to mention that if you're using LIKE and don't have any wildcards (e.g. %), then what you really want to be using is =. Like so:

if($_POST['County'] == 1) 
$where1 = 'County = \'Derbyshire\'';