2011-07-08 33 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 ='...'」。 –