0

I have the following query:-

SELECT
make_ID,
mileage
FROM cars
WHERE make_ID = ?
ORDER BY ?

The query works fine, except for the parameter binding. This is my prepared statement code:-

$stmt = sqli::$link->prepare($sql);

$make_filter = 1;
$sort_filter = "mileage";
$stmt->bind_param( 'is', $make_filter, $sort_filter );
$result = array();

// execute query
if ($stmt->execute()){
    // $stmt->store_result();

    // bind results to variables
    $stmt->bind_result($make, $mileage );

    // build results array for return
    while ($stmt->fetch()){
        echo("$make, $mileage<br>");
    };

    // clear memory
    $stmt->free_result();
}

The first bind WHERE make_ID = ? works fine and the results only show the rows with make_ID 1 as expected.

However, the second bind ORDER BY ? does not order the results based on mileage. I tested the SQL by removing the second ? and putting the value in manually like ORDER BY mileage - which works, so I know the query is not broken. But when trying to bind the value, the ordering doesn't work.

Can anybody see anything wrong with the code that might cause this? Thanks for your time.

1 Answers1

0

Scratch that. After a bit of stackoverflow browsing, it seems column names cant be bound, so ORDER BY won't work.

This post helped me out:- Doesn't seem to 'order by' for MySqli Php