Answer the question
In order to leave comments, you need to log in
How to designate sorting type?
Good afternoon!
I am sending a request from a client to have the items sent to me in a specific order. The backend sends a request to the database.
I have several sorting options:
-
descending price
- ascending price
- date added
- rating
Will 1,2,3,4 work? Came to backend 4 - so we send a request to the database "by rating". Or is it better to send the string from the client immediately, something like "order by 'price'"? Or some other way?
Answer the question
In order to leave comments, you need to log in
Pass the sort expression index. And according to this index, either execute the corresponding fully prepared query in advance, or build the query text based on the passed index.
Passing part of the request, which will then be embedded in the request text, is a bad idea, because it is fraught with an injection. A conditional expression based on the passed parameter does not have this drawback, but it will bury the possibility of using indexes for sorting if they can be used in a private query form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question