我的MySql查询中的致命问题

时间:2011-07-08 08:17:08

标签: php mysql

我对这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

1 个答案:

答案 0 :(得分: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";