php - PDO 中的 Postgresql 插入

标签 php postgresql pdo

我所有的脚本都是用 pdo-mysql 编写的。现在我正在迁移到 posgresql。我一直卡在插入查询中。

CREATE TABLE bus_spot
(
    bus_id integer NOT NULL,
    spot_loc_id integer NOT NULL,
    bus_dir_id integer NOT NULL
)

使用了 php 查询

$Bus_Id = 579;
$Bus_Dir_Id = 3;
$User_Loc_Id = 465;

$ISp_Res = $pdo->prepare("INSERT INTO bus_spot(bus_id, bus_dir_id, spot_loc_id) VALUES(?, ?, ?)");
$ISp_Res->execute(array($Bus_Id, $Bus_Dir_Id, $User_Loc_Id));

尝试插入这一行时出现以下错误

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: \"\""}

最佳答案

在我看来,Postgres 对整数的输入格式有点挑剔。尝试使用适当的类型绑定(bind)参数,而不是将它们传递给隐式使用字符串的 execute() 方法

$ISp_Res = $pdo->prepare(...);

$ISp_Res->bindParam(1, $Bus_Id, PDO::PARAM_INT);
$ISp_Res->bindParam(2, $Bus_Dir_Id, PDO::PARAM_INT);
$ISp_Res->bindParam(3, $User_Loc_Id, PDO::PARAM_INT);

$ISp_Res->execute();

关于php - PDO 中的 Postgresql 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22920115/

相关文章:

asp.net-mvc - 使用 Fluent NHibernate 构建数据库表

sql - 当一张表没有记录时进行多表查询

php - 为什么插入查询结果不显示在数据库中?

sql-server - SQL Server : Database stuck in “Restoring” state with PHP

php - 我们应该始终绑定(bind)我们的 SQL 语句吗?

php - 通过 id 和 parent_id 删除

PHP 错误输出 - 带有 IIS7 和 FastCGI 的 Plesk 9.2

PHP 脚本在 45 秒后超时

php - 在 IF 条件下与 int 和 string 进行比较

postgresql - 在对象数组上执行异步 pg-node 查询