2012-02-19 12 views
1

我试图运行一个简单的查询与建立在PDO之上的ORM。简单的PHP/MySQL的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 
+0

我没有跨过一个来。我可以通过删除where()子句中的一些并重新添加它们来调试,或者可以通过向ORM类代码中添加一些调试语句来进行调试。 – halfer 2012-02-19 20:30:02

回答

2

readtime在MySQL的reserved words

你必须重新命名列,或环绕columnn名反引号:

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

(提供的ORM允许,当然)

+0

好的电话,但这似乎不是问题。将该列重命名为“timedate”,并得到相同的错误。 – element119 2012-02-19 20:37:05

+0

@Archio'READ'也是一个保留字,请尝试重命名以及 – 2012-02-19 20:38:00

+0

就是这样,谢谢!哇,两个保留字,下一次检查MySQL手册的时间。 – element119 2012-02-19 20:42:38