简体   繁体   English

可能的MySql语法问题

[英]Possible MySql syntax issue

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. 我正在通过php访问数据库,该数据库列出了各州及其所属城市/城镇,并且我有一个JavaScript脚本将其显示为菜单。 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. 每个州的ID都会增加100,000(在数据库中,“阿拉巴马州”的值为100,000,而“阿拉斯加”的值为200,000。)因此,我在MySQL中测试了查询并正常工作,并测试了被发送到php脚本,这是正确的脚本,但是返回的菜单包含的值比应有的更多。

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. 其中state是先前选择的状态的ID。 The only thing I could think of is that the AND isn't working properly? 我唯一能想到的是AND不能正常工作?

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. 您还应该将此参数化,但这是另一个问题。

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

...WHERE 200,000 > 100,000 AND 200,000 < 100,000 + 100,000 ... 200,000> 100,000和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. 如果state的值= 100,000并且代码为200,000,则第一个布尔值评估为true,但是现在您检查的是code(200,000)是否小于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]) 换句话说,您要检查200,000 <200,000,所以您的检查是WHERE(TRUE [第一个表达式]和FALSE [第二个表达式])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM