Skip to content Skip to sidebar Skip to footer

Select Query's Order Not Working In Prepared Statement

I am created a prepared select query and it appears the query is not picking up the DESC or I have the bind_param structured wrong. I am trying to get the last id of the user_id's

Solution 1:

I don't think you can :

  • Use placeholders in an order by clause
  • Bind column names : you can only bind values -- or variables, and have their value injected in the prepared statement.

You can use number instead of field name in the 'order by' clause

Solution 2:

Why you have put ? after "order by" statement?

Your order by should reference to either id of your "profile_img" table or any timestamp field in that table...

e.g. $sql = " SELECT * FROM profile_img WHERE user_id = ? ORDER BY id DESC LIMIT 1 ";

here replace id (i am assuming this name) with the primary key field name of profile_image table

or

$sql = "
 SELECT *
 FROM profile_img
 WHERE user_id = ?
 ORDER BY created_on DESC LIMIT 1
";

here created_on (which i have also assumed) can be replaced by any timestamp field if you any in profile_img table

Post a Comment for "Select Query's Order Not Working In Prepared Statement"