php - 简单的 PHP/MySQL ORM 代码不执行

标签 php mysql pdo idiorm

我正在尝试使用构建在 PDO 之上的 ORM 运行一个简单的查询。

这是我要运行的代码:

$message = ORM::for_table("messages")
                    ->where("to_user_id", $user_id)
                    ->where("deleted", 0)
                    ->where("reply_id", $message_id)
                    ->where("read", 0)
                    ->order_by_desc("time")
                    ->limit(1)
                    ->count();

(这是使用 j4mie 的 Idiorm,https://github.com/j4mie/idiorm )

此代码似乎可行,但我收到以下 MySQL 错误:

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 'read = '0' ORDER BY time DESC LIMIT 1' 
        at line 1' in /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php:492 
    Stack trace: 
        #0 /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php(492): PDOStatement->execute(Array) 
        #1 /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php(289): ORM->run() 
        #2 /Users/chromium/Documents/root/e119/app/models/Message.class.php(73): ORM->count() 
        #3 /Users/chromium/Documents/root/e119/app/views/Messages/IndexView.php(42): Message::conversation_changed('3', '4', true) 
        #4 /Users/chromium/Documents/root/e119/app/templates/GameTemplate.php(13): require('/Users/chromium...') 
        #5 /Users/chromium/Documents/root/e119/lib/classes/Load.class.php(83): require('/Users/chromium...') 
        #6 /Users/chromium/Documents/root/e119/app/controllers/M on line 492 of /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php

最佳答案

readtimereserved words在 mySQL 中。

您必须重命名列,或在列名周围加上反引号:

 ->order_by_desc("`time`")
 ->where("`read`", 0)

(当然,前提是 ORM 允许这样做。)

关于php - 简单的 PHP/MySQL ORM 代码不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352913/

相关文章:

php - Codeigniter 菜鸟问题

php - PDO:调用非对象上的成员函数 fetch()

php - 自动换行不让数字也换行

php - 每月在特定日期向客户收费

php - MySQL:向当前用户显示帖子提要和帖子喜欢

MySQL 为用户连接并提取正确的数据

python - sqlalchemy + mysql,增量键重用

shell - 使用expect、ssh、ant和mysqldump时正确转义引号和括号

php - 带有 PDO 的混合 UTF-8 和 latin1 表

javascript - Jquery ajax将变量发布到php