2012-07-07 51 views
1

这里是PHPSQL语法错误。看起来像我错过了什么

$query = sprintf("SELECT SUM(value) AS totalvalue 
       FROM (
       SELECT * 
       FROM answers 
       WHERE user_id='%s' 
       AND test_id ='%s' 
       ORDER BY answers.id DESC 
       LIMIT '%s' 
       ) 
       AS subquery", 
       $user_id, 
       mysql_real_escape_string($test_id), 
       $num_of_q); 

这里结合我的SQL代码的错误:

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 ''40') AS subquery' at line 8

有什么问题吗?从限值

+0

只是旁注:因为你只需要'值'列,不要在子查询中使用'select *'。在那里使用'select value',为引擎提供更多优化查询的可能性 – 2012-07-07 08:24:08

回答

3

删除'',并使用数字时使用%d

$query = sprintf("SELECT SUM(value) AS totalvalue 
      FROM (
      SELECT * 
      FROM answers 
      WHERE user_id='%s' 
      AND test_id ='%s' 
      ORDER BY answers.id DESC 
      LIMIT %d 
      ) 
      AS subquery", 
      $user_id, 
      mysql_real_escape_string($test_id), 
      $num_of_q); 
1

您不需要引号LIMITLIMIT需要后面的数字,而您提供了一个字符串。

将其更改为:

LIMIT %d 

我建议你做同样与test_iduser_idWHERE条款,如果他们是类型INT

2

我想你扫描限值为字符串。阅读它作为int值。这将解决我认为的问题

相关问题