// url: http://localhost/asdf/?sort=credits
if(isset($_GET['sort'])){
$sort = $_GET['sort'];
}
$statement = $db->prepare("SELECT * FROM myTable ORDER BY :sort");
$statement->bindParam(':sort', $sort, PDO::PARAM_STR, 8);
var_dump($statement);
//object(PDOStatement)[2] public 'queryString' => string 'SELECT * FROM table1 ORDER BY :sort' (length=36)
$statement->execute();
我正在尝试从获取参数中选择排序选项,有人可以帮我吗?
按照@Uchiha Madara 的建议进行更新,我创建了一些预定义的排序字符串
if(isset($_GET['sort'])){
switch ($_GET['sort']){
case 'val1':
$sortName = 'val1';
break;
case 'val2':
$sortName = 'val2';
break;
default:
$sortName = false;
break;
}
}
if($sortname != false){
// then prepare query
$statement = $db->prepare("SELECT * FROM table1 ORDER BY ?");
$statement->execute(array($sortName));
}
但结果仍然相同,订购选择被忽略
最后更新!
$sort = 'val1';
$orderList = array(
'val1',
'val2'
);
// then prepare query
$statement = $db->prepare("SELECT * FROM myTable ORDER BY $sort");
$statement->execute();
感谢@Madara Uchiha 和@Ray!