order by pid ? (asc|desc) placeholders statement

I'm thinking that this "problem" can not be solved. I have in my code not short querys.

Depending on the value of the variable, I need to sort the date by ASC or DESC.

I do not want to copypaste querys. I thought that i can work as usual variables, but it's not. (?)

How can (whether it is possible at all) to substitute in the query ASC or DESC, as well as variables?

query prepare error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? limit ? offset ?' at line 1

simply example:

...
$sort='asc';
...
select pid,title_posts,txt_posts,count_postvs,usname from posts join usopt on uid_posts=uid_usopt 
where uid_posts=? and postacc=1 order by pid ? limit ? offset ?

mysqli_stmt_bind_param($rawdata, 'isii', $uid, $sort, $_SESSION['r2sh'], $offset);
...

You can do this using a conditional expression in the order by:

order by (case when ? = 'asc' then pid end) asc,
         pid desc

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image