从 PHP (PDO) 更新 MySQL 类型的列
Update MySQL Columns of type SET from PHP (PDO)
我想使用包含 SET
类型列的用户帐户向 table 添加数据
roles set('user','admin') NOT NULL
在 PHP 中,值存储在字符串的索引数组中。
我正在绑定参数
$st = $conn->prepare($q);
...
// $pv is the value which causes the problem.
$r = array('user', 'admin');
// Nothing of these worked
// 1. Attempt, pass as the original array hoping PDO will take care
$pv = $r;
// 2. Convert to string with separator in various variations.
$pv = '\''.implode(',', $r).'\'';
// 2b.
$pv = '(\''.implode(',', $r).'\')';
// 2c.
$pv = '(\''.implode('\',\'', $r).'\')';
// 2d.
$pv = '\''.implode('\',\'', $r).'\'';
$st->bindParam(':roles', $pv);
我也试过了
$conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, FALSE);
始终执行查询returns
"01000",1265,"Data truncated for column 'roles' at row 1"
解决方案是:
$r = array('user', 'admin');
$pv = implode(',', $r);
$st->bindParam(':roles', $pv);
我想使用包含 SET
类型列的用户帐户向 table 添加数据roles set('user','admin') NOT NULL
在 PHP 中,值存储在字符串的索引数组中。
我正在绑定参数
$st = $conn->prepare($q);
...
// $pv is the value which causes the problem.
$r = array('user', 'admin');
// Nothing of these worked
// 1. Attempt, pass as the original array hoping PDO will take care
$pv = $r;
// 2. Convert to string with separator in various variations.
$pv = '\''.implode(',', $r).'\'';
// 2b.
$pv = '(\''.implode(',', $r).'\')';
// 2c.
$pv = '(\''.implode('\',\'', $r).'\')';
// 2d.
$pv = '\''.implode('\',\'', $r).'\'';
$st->bindParam(':roles', $pv);
我也试过了
$conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, FALSE);
始终执行查询returns
"01000",1265,"Data truncated for column 'roles' at row 1"
解决方案是:
$r = array('user', 'admin');
$pv = implode(',', $r);
$st->bindParam(':roles', $pv);