Reputation: 263
I'm trying to display 10 rows and order them by the highest score first, the code runs with no errors but it's still just displaying them in the order they appear in the database.
$questionScore = query('SELECT * FROM questions ORDER BY score = :score DESC LIMIT 10',
array('score' => ['score']),
$conn);
This is my query statement -
function query($query, $bindings, $conn)
{
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
$results = $stmt->fetchAll();
return $results ? $results : false;
}
My function used
Upvotes: 0
Views: 346
Reputation: 212402
It's your function that's forcing you to set a binding when it isn't necessary
$questionScore = query(
'SELECT * FROM questions ORDER BY score DESC LIMIT 10',
array(),
$conn
);
function query($query, $bindings, $conn)
{
$stmt = $conn->prepare($query);
if (!empty($bindings))
$stmt->execute($bindings);
$results = $stmt->fetchAll();
return $results ? $results : false;
}
or even reorganise the arguments to your function, so you don't need to pass empty bindings if they're not needed:
$questionScore = query(
'SELECT * FROM questions ORDER BY score DESC LIMIT 10',
$conn
);
function query($query, $conn, $bindings = array())
{
$stmt = $conn->prepare($query);
if (!empty($bindings))
$stmt->execute($bindings);
$results = $stmt->fetchAll();
return $results ? $results : false;
}
Upvotes: 1