疯了:Mysql查询不会工作;但非常简单

//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并将其变量替换为相应的字符串,查询就可以工作。疯了:Mysql查询不会工作;但非常简单

我在另一个页面上也使用了相同的确切结构,在foreach(我知道效率不高)中的查询使用了相同的参数。列名是正确的。快要疯了。你看到一个错误?

更新:

为了测试这个PHP页面我试着查询:

$zip_query = mysql_query("SELECT * FROM zip_codes WHERE state = 'california'") or die(mysql_error());

和它的工作。一些$状态变量='california'。已经尝试了strip_tagsmysql_real_escape_string

var_dump将返回:

string(10) "California" 

和查询失败。但如果我手动输入state = 'California',则查询起作用。

回答:

尝试做一个var_dump()在您查询之前的$city$state

这可能是因为它们是空的或在其中有引号。

回答:

我想在这里您可以通过忽略圆括号犯了一个错误:

"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')" 

回答:

运行时出现错误吗? 如果查询错误,则必须有一些mysql错误。 尝试改变这一点。

$zip_query = mysql_query('SELECT * FROM zip_codes WHERE city = \''.$city.'\' AND state = \''.$state.'\' OR city = \''.$city.'\' AND full_state = \''.$state.'\') ; 

也检查$城市$状态等在适当的情况下,在DATABSE

回答:

full_state = '$state)"

什么缺失? :)

回答:

我总是喜欢把我的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命令行中测试您的查询(如调试行所示)。

回答:

应使用mysql_real_escape_string逃脱你的价值观,而不是用strip_tags:

$city = mysql_real_escape_string($location_broken3[0]); 

$state = mysql_real_escape_string($location_broken3[1]);

以上是 疯了:Mysql查询不会工作;但非常简单 的全部内容, 来源链接: utcz.com/qa/261153.html

回到顶部