我在php / mysql中遇到了我的脚本问题。这是服务器显示的错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists (select * from notificacoes where uid in () order by id desc' at line 1' in C:\wamp\www\bigui\classes\Notificacoes.class.php on line 57
这是我的PHP代码:
static function listar(){
$strIdAmigos = Amizade::$strIdAmigos;
$query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc');
return $query->fetchAll(PDO::FETCH_ASSOC);
}
我在mysql中的表是空的,没有值。当我在其中插入一个值时,错误就会消失,一切都很好。有什么帮助吗?
答案 0 :(得分:1)
如果$strIdAmigos
为空,则会导致语法错误。
在执行此查询之前,您应该检查$strIdAmigos
值是否为空以避免此问题。如果需要,不要忘记逃避价值观。
答案 1 :(得分:0)
如果您在变量$strIdAmigos
中没有任何内容运行查询,则会出错。
在运行查询之前,尝试初始化和/或检查变量$ strIdAmigos:
$strIdAmigos = "";
if (empty($strIdAmigos)) {
/* Uh oh, throw an error */
} else {
$query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc');
}
请注意,如果$strIdAmigos = "0"
,empty($strIdAmigos)
仍将评估为true,因此不会运行查询。