2011-07-08 35 views
-1

我有关于这3个查询一个问题:致命的问题查询

select count(*) as rec_count from test_history inner join 
    test_detail on test_history.history_id = test_detail.test_history_id 
    where test_history.history_id in ({$_SESSION['history_ids']}) 
    and test_history.user_id = $userID 

select count(*) as correct_answers from test_history inner join test_detail 
    on test_history.history_id = test_detail.test_history_id 
    where test_history.history_id in ({$_SESSION['history_ids']}) and 
    test_history.user_id = $userID and is_correct = 1 

而这一次

select * from test_topics inner join 
    test_topics_link on test_topics.`topic_id` = test_topics_link.`topic_id` 
    where test_topics_link.test_id in ({$_SESSION['ids_tests']}) and percentage > 0 

我一直都想与此错误:

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 ') and 
test_history.user_id = 82' at line 3 

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 ') and 
test_history.user_id = 82 and is_correct = 1' at line 2 

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 ')' at line 1 

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 ') and percentage > 0' at 
line 3 
+0

什么值{$ _SESSION ['history_ids']}?确保这个扩展为一个合理的值 – wonk0

+0

显示为php代码。 + var_dump($ _ SESSION ['ids_tests'],$ _SESSION ['history_ids']) – Subdigger

+0

是的它有正确的值它有正常的用户ID 82 – Marco

回答

2

由于$_SESSION['ids_tests'],我想是一个数组,你应该写implode(',',$_SESSION['ids_test']),例如,在PHP中它应该是:

$query = "select * from test_topics inner join 
     test_topics_link on test_topics.`topic_id` = test_topics_link.`topic_id` 
     where test_topics_link.test_id in (". 
     implode(',',$_SESSION['ids_tests']).") and percentage > 0"; 
+0

没有这个不工作的全部 – Marco

+0

因为你使用的语法是“where column in(...)”,所以我认为你知道这个语法和值列表一起工作。也就是说,只要$ _SESSION ['ids_tests'])是一个数组,我发布的代码就可以工作。否则,您不应该使用该语法并改用“where column ='...'”。 –