我在 PostgreSQL 数据库中有一个列,它是一个 bigint。
到目前为止,我无法找到一种在 PHP 中制定 PDO 插入的方法。
PHP 预定义常量仅列出PDO::PARAM_INT
. 当我在我的声明中使用它时,我收到以下错误。请注意,即使我没有在bindValue
语句中包含数据类型,我也会收到相同的错误。据我了解,通过省略 PHPPDO::PARAM_INT
基于变量类型使用的类型。
带有消息“SQLSTATE [22003]”的未捕获异常“PDOException”:数值超出范围:7 错误:值“2978878787878787878”;超出 index.php:163 中整数类型的范围
如何使用 PDO 在 MySQL 中存储 BIGINT?是一个问题here它谈到了同样的问题。但是,他们直接使用字符串作为数据库列类型。虽然这是一个很好的解决方法,但如果您无法更改数据库类型,它不会考虑在内。
有没有一种方法可以在 PHP 中使用 bigint 和 PDO,而不需要修改数据库?
示例代码
$query = "INSERT INTO clients ";
$query .= "(timestamp, name, value) ";
$query .= "VALUES (now(), :name, :value) ";
$query .= "RETURNING id";
$name = "Bob Probert";
$value = 2978878787878787878; // note this is less than bigint max of 9223372036854775807
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->bindValue(':value', $value, PDO::PARAM_INT);
桌子
Table "db.clients"
Column | Type | Modifiers | Storage | Stats target | Description
---------------+-----------------------------+------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('tickets_id_seq'::regclass) | plain | |
timestamp | timestamp without time zone | | plain | |
name | character varying(40) | | extended | |
value | bigint | not null | plain | |
Indexes:
"tickets_pkey" PRIMARY KEY, btree (id)