-1

I'm not sure either prepared statements are not capable of ordering data or I'm just bad at this, I've got this code:

$by_arrangement     = $_POST['filter_by_arrangement'];
$by_date            = $_POST['filter_by_date'];

$sql = "SELECT id,title,description,champion
        FROM our_videos
        WHERE datemade BETWEEN NOW() - INTERVAL ? DAY AND NOW()
        ORDER BY ?
        LIMIT 10";

$stmt = mysqli_prepare($db_conx, $sql);
mysqli_stmt_bind_param($stmt, "is", $by_date, $by_arrangement);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $single_id, $single_title, $single_description, $single_champion);
while (mysqli_stmt_fetch($stmt))
{
    //GETTING RESULTS
}
mysqli_stmt_close($stmt);

Everything works perfect on arranging by date, though it's not working ORDER BY $by_arrangement,which can have following outcomes:

datemade DESC
datemade 
views DESC 
views 
comment_count DESC
comment_count

but it always orders by id for some reason... What could possibly be the problem? Thanks

Donny123
  • 105
  • 6

1 Answers1

0

You order by a colomn, which cannot be a binded variable.

I'll see if i can find a reference.

I have found this: With mysqli and prepared statements can I PASS IN COLUMN NAMES to 'ORDER BY'

It contains an useful example for you.

Community
  • 1
  • 1
KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • *"I have found this: With mysqli and prepared statements can I PASS IN COLUMN NAMES to 'ORDER BY'"* - Which should have been marked as a duplicate. Including the one I used to close the question. – Funk Forty Niner Feb 21 '15 at 15:23