PHP PDO将sql语句的一部分绑定到某些东西上


PHP PDO Binding part of the sql statement to something

为什么我不能这样做(这是在一个函数内):

switch($a) {
case 'userStatus':
break;
case 'userCanEdit':
break;
case 'userCanView':
break;
default:
echo 'Unable to identify type';
return false;
$sqlUpdateId = 'update users set :sql = :status where userId = :id and code = :code';
$updateId->bindParam(':sql',$a);
}

注意到:sql部分吗?它设置在上面,通过开关后通过。但这行不通。只能绑定输入值吗?这将节省大量代码。

但是您可以这样做:

$allowedColumns = ('userStatus', 'userCanEdit', ...);
if (in_array($a, $allowedColumns)) {
    $sqlUpdateId = 'update users set ' . $a . ' = :status where userId = :id and code = :code';
}

不,不能绑定列名,只能绑定值。