//DB connect is here
foreach ($find as $listing) {
//bunch of hooblah that discovers $state and $city
//this and below is all you need to see really
$city = strip_tags($location_broken3[0]);
$state = strip_tags($location_broken3[1]);
print $city; //THIS WORKS...
print $state; //AS DOES THIS!
//tried this also
$city1 = settype($city, 'string');
$state1 = settype($state, 'string');
$zip_query = mysql_query("SELECT * FROM zip_codes WHERE city = '$city'
AND state = '$state' OR city = '$city' AND full_state = '$state'
") or die(mysql_error());
if ($row = mysql_fetch_array($zip_query)) {
die("<h1><b>Query Working!!</b></h1>");
}
}
$zip_query
返回零行。如果我将其复制/粘贴到phpmyadmin并将变量替换为相应的字符串,则查询有效。
我在另一个页面上也使用了相同的结构,foreach
内的查询(我知道效率不高)具有相同的参数。列名是正确的。快要疯了。你看到错误吗?
更新
为了在这个php页面上测试查询,我尝试了:
$zip_query = mysql_query("SELECT * FROM zip_codes WHERE state = 'california'") or die(mysql_error());
它有效。一些$ state变量='california'。已经尝试了strip_tags
和mysql_real_escape_string
。
var_dump
将返回:
string(10) "California"
查询失败。但如果我手动输入state = 'California'
,查询就会起作用。
答案 0 :(得分:5)
我猜你在这里省略了括号:
"WHERE city = '$city' AND state = '$state' OR city = '$city' AND full_state = '$state'"
你的意思是:
"WHERE city = '$city' AND state = '$state' OR (city = '$city' AND full_state = '$state')"
这可以简化为:
"WHERE city = '$city' AND (state = '$state' OR full_state = '$state')"
答案 1 :(得分:0)
在查询之前尝试在var_dump()
和$city
上执行$state
。
可能是他们是空的或者在他们或其他东西中有引号。
答案 2 :(得分:0)
运行时会出现任何错误吗? 如果查询错误,必定会出现一些mysql错误。 尝试改变一下。
$zip_query = mysql_query('SELECT * FROM zip_codes WHERE city = \''.$city.'\' AND state = \''.$state.'\' OR city = \''.$city.'\' AND full_state = \''.$state.'\') ;
还要检查$ city和$ state等是否在数据库中
答案 3 :(得分:0)
full_state = '$state)"
什么不见了? :)
答案 4 :(得分:0)
我总是喜欢把我的SQL查询放到一个格式中,这样我就可以在调试时记录它或显示它。
$fmt = 'SELECT * FROM zip_codes'
. ' WHERE ( city = "%1$s" AND state = "%2$s" )'
. ' OR ( city = "%1$s" AND full_state = "%2$s" )';
$query = sprintf($fmt, $city, $state);
print "<br/>DEBUG: query = <tt>$query</tt><br/>\n";
$result = mysql_query($query);
...
然后,如果您仍然想知道,请在MySQL命令行中测试您的查询(如调试行所示)。
答案 5 :(得分:0)
你应该使用mysql_real_escape_string来转义你的值而不是strip_tags:
$city = mysql_real_escape_string($location_broken3[0]);
$state = mysql_real_escape_string($location_broken3[1]);