加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – PostgreSQL PDO插入数组类型

发布时间:2020-12-13 16:51:21 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用 PHP PDO将值插入PostgreSQL数据库,但遇到以下错误消息: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: array value must start with "{" or dimension information 字段类型是PostgreSQL Array. 这是我的代码(简化): try { $sql
我正在尝试使用 PHP PDO将值插入PostgreSQL数据库,但遇到以下错误消息:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: array value must start with "{" or dimension information

字段类型是PostgreSQL Array.

这是我的代码(简化):

try {
  $sql='INSERT INTO table (fieldName,foo) VALUES (?,?)';
  $fieldName=array('1','2');
  $data=array($fieldName,'bar'); # fieldName is array type in PostgreSQL
  $STH = $conn->prepare($sql);          
  $STH->execute($data);
catch(PDOException $e) {
  # Handle exception
}

谢谢!

解决方法

万一有人遇到过这种情况,解决办法是内爆数组并添加{}.

$fieldName='{'.implode(",",$fieldName).'}';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读