0

I'm trying to figure out why my query isn't working, but it doesn't make sense to me. I'm accessing a database via php that lists states and their affiliated cities/towns, and I have a javascript script which displays them as menus. Each state has an ID which is incremented by 100,000 (meaning in the database, "Alabama" is 100,000 and "Alaska" is 200,000.) So I tested my query out in MySQL and it works properly, and I tested the javascript value that was being sent to the php script and it was the correct one, but the returned menu is containing many more values than it should.

My query looks like:

mysql_query("SELECT * FROM fullstates WHERE code > '$state' AND code < '$state' + 100000");

where state is the ID of the previous state that was picked. The only thing I could think of is that the AND isn't working properly?

2
  • 1
    Can you post some existing data and expected results, as well as the table/column definition? Commented Dec 6, 2011 at 1:55
  • 1
    Also, if code is a multiple of 100000, then both code > $state and code < $state + 100000 will fail! Commented Dec 6, 2011 at 1:57

2 Answers 2

1

Are your codes numeric or text? Seems like they should be numeric, in which case you should drop your single quotes:

mysql_query("SELECT * FROM fullstates WHERE code > $state AND code < ($state + 100000)");

You should also parameterize this, but that's another concern.

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct sir! Knew it had to be a syntax issue, and yes I realize to group them as you did as well. Thanks!
0
mysql_query("SELECT * FROM fullstates WHERE code > '$state' AND code < '$state' + 100000");

...WHERE 200,000 > 100,000 AND 200,000 < 100,000 + 100,000

if the value of state = 100,000 and code is 200,000, then the first boolean evaluation is true, but now you check if code(200,000) is LESS than state(100,000) + 100,000.

In other words you're checking if 200,000 < 200,000, so your check is WHERE (TRUE[first expression] AND FALSE[second expression])

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.