使用 PHP PDO for mysql 更新具有空值的不可空字段时,我无法生成错误或异常。直接执行 sql 会产生预期的错误。
PDO 中的所有内容都会导致status_id字段的值设置为 0,而不是表示该字段不允许为空的异常或错误。
$stmt_handler = $this->db_handler->prepare(
"UPDATE faxes SET metadata = :metadata, status_id = :status_id,
created = :created, updated = :updated, content = :content,
vendor_fax_id = :vendor_fax_id WHERE id = :id");
$stmt_handler->bindParam(':id', $fax->id);
$stmt_handler->bindParam(':metadata', $fax->metadata);
$stmt_handler->bindParam(':status_id', $fax->status_id); // tried different combinations
$stmt_handler->bindParam(':created', $fax->created);
$stmt_handler->bindParam(':updated', $fax->updated);
$stmt_handler->bindParam(':content', $fax->content);
$stmt_handler->bindParam(':vendor_fax_id', $fax->vendor_fax_id);
$stmt_handler->execute();
我尝试了不同的组合并将 PDO::ATTR_EMULATE_PREPARES 设置为 false (正如这个问题PHP mysql PDO 拒绝设置 NULL 值所建议的那样)
我的原始绑定:
bindParam(':status_id', $fax->status_id);
试过
bindValue(':status_id', null, PDO::PARAM_INT);
bindValue(':status_id', null, PDO::PARAM_NULL);
bindValue(':status_id', 'NULL', PDO::PARAM_INT);
bindValue(':status_id', 'NULL', PDO::PARAM_NULL);
bindValue(':status_id', null);
bindValue(':status_id', 'NULL');
PHP版本:PHP 5.3.10-1ubuntu3
MYSQL 服务器版本:5.5.28-0ubuntu0.12.10.1
编辑每条评论
mysql> show columns from faxes;
+---------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+-------------------+-----------------------------+
| id | char(36) | NO | PRI | NULL | |
| vendor_fax_id | char(36) | YES | UNI | NULL | |
| metadata | varchar(255) | NO | | NULL | |
| status_id | int(10) | NO | MUL | NULL | |
| created | datetime | NO | | NULL | |
| updated | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| content | mediumblob | NO | | NULL | |
+---------------+--------------+------+-----+-------------------+-----------------------------+
7 rows in set (0.00 sec)