2

我如何为mysql中的两个单独字段设置顺序

代码

$sql = 'SELECT q.question_id, a.answer_id, q.question_name, a.option_name, q.display_order AS ques_order, a.correct, a.display_order AS opt_order FROM questions q JOIN answers a ON (a.question_id = q.question_id) WHERE test_id = '.$m_test_id.' ORDER BY ques_order, opt_order ASC'; 

错误信息:

MysqlError: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的“ORDER BY ques_order, opt_order ASC”附近使用正确的语法

ORDER BY ques_­order,­ opt_­order ASC;
4

1 回答 1

4

有什么问题$m_test_id; 它可能是空的并导致 SQL 语法出现问题。考虑使用准备好的语句:

$sql = 'SELECT q.question_id, a.answer_id, q.question_name, a.option_name, q.display_order AS 
    ques_order, a.correct, a.display_order AS opt_order 
    FROM questions q 
    JOIN answers a ON a.question_id = q.question_id 
    WHERE test_id = :test
    ORDER BY ques_order, opt_order ASC'; 

$stmt = $db->prepare($sql);
$stmt->execute(array(':test' => $m_test_id));

除此之外,您应该首先找出为什么$m_test_id是空的。

如果您不使用PDOmysqli,则可以使用mysql_real_escape_string(),但请注意它已被弃用,不应再使用。

于 2013-05-13T07:45:47.637 回答