sql - how can we assign a ORDER BY in two seperate fields in MySql query? -
how set order 2 separate fields in mysql
code
$sql = 'select q.question_id, a.answer_id, q.question_name, a.option_name, q.display_order ques_order, a.correct, a.display_order opt_order questions q join answers on (a.question_id = q.question_id) test_id = '.$m_test_id.' order ques_order, opt_order asc';
error message:
mysqlerror: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'order ques_order, opt_order asc' @ line 4
order ques_order, opt_order asc;
there's wrong $m_test_id
; it's empty , causing issues sql grammar. consider using prepared statements:
$sql = 'select q.question_id, a.answer_id, q.question_name, a.option_name, q.display_order ques_order, a.correct, a.display_order opt_order questions q join answers on a.question_id = q.question_id test_id = :test order ques_order, opt_order asc'; $stmt = $db->prepare($sql); $stmt->execute(array(':test' => $m_test_id));
besides that, should find out why $m_test_id
empty in first place.
if you're not using pdo or mysqli can use mysql_real_escape_string()
note it's deprecated , shouldn't used anymore.
Comments
Post a Comment