将phpGrid中的字符串转换为数组,以便插入到postgresql中


Converting a string into array in phpGrid for inserting into postgresql

我试图使用phpGrid与PostgreSQL数据库插入一个字符串到整数数组。当在网格中显示数据以供查看时,我必须将数组转换为字符串以删除括号,但是当我试图在添加字段后将字符串转换回数组时,它没有转换为格式{1,2,3,4}。我使用的是PostgreSQL提供的string_to_array函数。下面是我使用的代码:

$dg = new C_DataGrid("SELECT array_to_string(field, ', ') as field_to_string FROM tblname");

按预期工作(从显示的数据中删除{}),但是当我尝试插入时,这个查询看起来像这样:

INSERT INTO tblname (field_to_string) VALUES ('1,2,3,4');

这就是我要插入的(转换为数组)

$arrFields['field_to_string'] = "string_to_array('" . $arrFields['field_to_string'] . "', ',')";
$sqlCrud = $db->db->GetInsertSQL($rs, $arrFields, get_magic_quotes_gpc(), true);

任何帮助都会很感激。

谢谢。

可以回显sqlCrud并通过将其DEBUG设置为true来查找SQL Insert语句吗?很可能是Insert语句在转换过程中出错。

您的PostgreSql应该类似于以下内容:

INSERT INTO tblname(field_to_string) VALUES ('{1,2,3,4}'::varchar[])

你可以在这里查看PostGreSql的数组到字符串转换示例:http://www.postgresonline.com/journal/archives/228-PostgreSQL-Array-The-ANY-and-Contains-trick.html