Mysql查询错误

时间:2012-01-05 18:43:31

标签: php mysql

此查询有什么问题?

$query2=mysql_query("SELECT * FROM $t_medici WHERE `oras` LIKE $orase") or die(mysql_error());

此代码返回

Unknown column 'Bucuresti' in 'where clause'

4 个答案:

答案 0 :(得分:3)

$orase需要引号。

$query2=mysql_query("SELECT * FROM $t_medici WHERE oras LIKE '$orase'") or die(mysql_error());

答案 1 :(得分:0)

$orase放在单引号内。 如果需要,请使用%符号。

答案 2 :(得分:0)

如果oras字段是字符串,那么$orase应该有单引号:

$query2=mysql_query("SELECT * FROM $t_medici WHERE `oras` LIKE '$orase'") or die(mysql_error());

当达到此行时,变量$orase包含字符串Bucuresti,因此SQL代码的结尾如此:

...WHERE `oras` LIKE Bucuresti

但你也应该逃避它(以防止SQL注入):

$query2=mysql_query("SELECT * FROM $t_medici WHERE `oras` LIKE '".mysql_real_escape_string($orase)."'") or die(mysql_error());

另外,我会在双引号字符串中添加大括号,即{$ t_medici}:

$query2=mysql_query("SELECT * FROM {$t_medici} WHERE `oras` LIKE '".mysql_real_escape_string($orase)."'") or die(mysql_error());

另一方面,如果你使用LIKE和你一样的字符串,那么返回的任何行都必须Bucuresti(或任何$ orase保持不变),在它之前或之后都没有(包括空格!)。在这种情况下,完全不使用LIKE;你也可以这样使用=

"SELECT * FROM {$t_medici} WHERE `oras` ='".mysql_real_escape_string($orase)."'"

但是如果你想在Bucuresti字段中以某种方式匹配任何具有oras(或任何$ orase保留)的行(例如“Bucuresti”,“Bucuresti”,“kdsbks kksd saBucurestie”,等)然后你应该像%这样使用:

"SELECT * FROM {$t_medici} WHERE `oras` LIKE '%".mysql_real_escape_string($orase)."%'"

即。如果要%Bucuresti% $orase,则Bucuresti

答案 3 :(得分:0)

从互联网复制查询并进行编辑时,非常熟悉的错误。单引号如上所述。