Home > Blockchain >  Does ORDER BY and WHERE clause have different precedence?
Does ORDER BY and WHERE clause have different precedence?

Time:02-06

I am currently fetching data using the statement

$queryfetchBookings="SELECT `id`,`sessionHeld`,`fk_student_id`,`channelName`,`video_duration`,`audio_duration`,`category`,`dateBooked`,`timeSlot`,`duration`,`category`,`studentName`,`overallRating`,`chat_duration`  FROM `one_to_one_bookings` WHERE fk_co_id=".$co_id;

I now want to add an

 ORDER BY `id` ASC 

to the end but whatever variation I have tried results in failure.(Does not fetch data). Need advice on how to proceed.

Different variations that i have tried: enter image description here

CodePudding user response:

First of all, you should use prepared statements to avoid SQL injection, and not concatenating strings to your query.

You are doing wrong your concatenation in PHP, you need to add a . and some quotes:

$queryfetchBookings="SELECT `id`,`sessionHeld`,`fk_student_id`,`channelName`,`video_duration`,`audio_duration`,`category`,`dateBooked`,`timeSlot`,`duration`,`category`,`studentName`,`overallRating`,`chat_duration`  
FROM `one_to_one_bookings` WHERE fk_co_id=".$co_id." ORDER BY `id` ASC";

The problem was at the end of the query:

WHERE fk_co_id=".$co_id." ORDER BY `id` ASC";

CodePudding user response:

I am not sure about how the server you are using works, but in oracle it would not work because after the WHERE clause you are adding a ";". At least in oracle this would mean that the query is over, so I am guessing this is why it is not considering your last sentence. I hope I can help :)

  •  Tags:  
  • Related