2017-10-09 182 views
0

我在想这个“问题”无法解决。 我在我的代码不短 querys。order by pid? (asc | desc)占位符声明

根据变量的值,我需要通过ASC或DESC对日期进行排序。

我不想copypaste查询。我认为我可以照常工作,但事实并非如此。 (?)

如何能(是否可能在所有)替换查询ASC或DESC,以及变量?

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); 
... 

回答

0

可以使用在order by条件表达式做到这一点:

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

有趣......我就知道这么一点关于MySQL :( –