0

可能重复:
如何使用 CodeIgniter 运行 mysqli 查询?

我有一个mysql查询,我试图从php运行。但它在codeigniter中给了我一个sql语法错误。当我复制粘贴相同的查询并通过用正确mysql workbench的值替换@uidand来运行它时,它工作正常。@rangee

这是查询:

$this->db->query("
            SET @uid    = ".mysqli_real_escape_string($conid,$userid).";
            SET @rangee = ".mysqli_real_escape_string($conid,$attempt)." * 50;

            PREPARE STMT FROM 
            '
            UPDATE     
                notificationrecievers 
                SET notificationrecievers.status=1 
                WHERE notificationrecievers.status=0 and notificationrecievers.recieverid=? and 

                notificationrecievers.notificationid <=
                (
                     SELECT MAX(notid) FROM
                     (
                      SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
                      nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
                      LIMIT 50 OFFSET ?
                     )e
                )   

               AND

                notificationrecievers.notificationid >=
                (
                     SELECT min(notid) FROM
                     (
                      SELECT n.notid FROM user u,notifications n,notificationrecievers nr WHERE
                      nr.recieverid=? AND u.userid=n.senderid AND nr.notificationid=n.notid ORDER BY n.notid DESC
                      LIMIT 50 OFFSET ?
                     )g
                );';
            EXECUTE STMT USING @uid,@uid,@rangee,@uid,@rangee;");

为什么它给我错误?

4

2 回答 2

1

首先,query()方法不打算执行多个查询。接下来,您的代码允许通过 $userid 变量进行注入。

最好从这些原始准备好的语句转移到二进制准备好的语句,如手册中所述

于 2012-12-25T19:53:45.890 回答
0

从您的代码中完全无法判断 $this->db 是什么类型的对象。如果是 mysqli 用于转义,则 query() 函数不支持多语句。

此外,当您使用 PHP 中的准备好的语句时,您应该使用 PHP 包装器作为准备好的语句,如mysqli::prepare()

于 2012-12-25T19:54:53.617 回答