我知道我可以使用 pg_query
将一个数组插入到 Postgres 数据库中。
INSERT into table (field) VALUES ('{{element, element},{}}')
但是使用 pg_insert
怎么样?
$array["field"] = "{{element, element},{}}";
$result = pg_insert($con, "table", $array);
字符串 {{element, element},{}}
实际上会作为 2D Postgres 数组插入到 field
中吗?
我一直想测试一下,但目前我没有任何 PostgreSQL DB 可以测试..
最佳答案
我刚刚运行了您的具体示例。
(1) 在 Postgres 中:
CREATE TABLE atable (afield text[][]);
(2) 在 PHP 中:
$array["afield"] = "{{'element', 'element'},{}}";
$result = pg_insert($this->conn, "atable", $array);
我得到了以下错误:
Notice: pg_insert(): Unknown or system data type '_text' for 'afield' in ...
我尝试使用数组值:将其设为 2x2 数组、一维数组等等 - 结果相同。我什至更改了表以将字段作为一维数组:text[]
并相应地更改了代码 - 我仍然得到相同的结果。
我开始进一步挖掘并在 PHP documentation 上找到以下内容对于 pg_insert
:
This function is EXPERIMENTAL. The behaviour of this function, its name, and surrounding documentation may change without notice in a future release of PHP. This function should be used at your own risk.
基本上,它有很多错误,不应该使用。有趣的是,使用
pg_query("INSERT INTO...")
工作正常。希望这能回答您的问题。 :)
关于PHP:Postgres 数组和 pg_insert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979327/