Is there a default ORDER BY value?
I have a dynamic mysql query and part is ORDER BY
hardcoded into the query.
The problem is I don't know if every request will have ORDER BY
, and I don't know which columns will be available for ordering.
Is there a default that I can write so that I can use
if (empty($order)){$order = "default_value"}
$query = "select ...... ORDER BY $order"
+3
d -_- b
source
to share
2 answers
As a result of requests, no default request is received. Unless you explicitly specify an ORDER BY clause, the data order is undefined. You can only specify a ASC DESC
few columns.
+4
Oleksi
source
to share
If you're not sure about the order, why not make it a conditional sentence?
$order_by = empty($order) ? "" : "ORDER BY " . $order
$query = "select ...... $order_by"
+2
Bogdan Rybak
source
to share