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:

mysql­error: have error in sql syntax; check manual corresponds my­sql 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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -