MySQL Select Statement Problem

Codes here !

Moderators: macek, egami, gesf

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: MySQL Select Statement Problem

Postby swirlee » Fri Jul 11, 2003 7:36 am

sallie_ann wrote: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:

Code: Select all

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


You're better off with this:

Code: Select all

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:

Code: Select all

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

Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest

cron