Поставить подготовленный оператор массивом

Могу ли я использовать подготовленный оператор в Postgres для добавления нескольких значений? Когда я увидел, что вещи добавлены в подготовленный оператор с array($val) , мне пришло в голову, что я могу предоставить массив значений, которые будут помещены в мою таблицу. Это дико неверно? Когда я попытался, я увидел в моей таблице db только Array . Я не знаю, является ли это фактическим массивом, но я предполагаю, просто слово, поскольку столбец является простой character variable .

 $tag = array('item1', 'item2', 'item3'); // Prepare a query for execution $result = pg_prepare($dbconn, "my_query", "INSERT INTO $table ($column) VALUES ($1)"); // Execute the prepared query. Note that it is not necessary to escape // the string "Joe's Widgets" in any way $result = pg_execute($dbconn, "my_query", array("$tag")); 

В противном случае, почему одно значение передается как массив?

Related of "Поставить подготовленный оператор массивом"

Нет, это не так, вы вставили текст Array … если тип $ column – текст, который должен прочитать ваш код

 $tag = array('item1', 'item2', 'item3'); // Prepare a query for execution $result = pg_prepare($dbconn, "my_query", "INSERT INTO $table ($column) VALUES ($1)"); // Execute the prepared query. Note that it is not necessary to escape // the string "Joe's Widgets" in any way foreach( $tag as $i ) $result = pg_execute($dbconn, "my_query", array($i)); /// alternatively you could try this if you really wanna insert a text as array of text without using text[] type - uncomment line below and comment the 2 above // $result = pg_execute($dbconn, "my_query", array(json_encode($tag))); 

или если вы определили $ column как text [], который является законным в postgresql как массив, код должен читать

 $tag = array('item1', 'item2', 'item3'); // Prepare a query for execution $result = pg_prepare($dbconn, "my_query", "INSERT INTO $table ($column) VALUES ($1)"); // Execute the prepared query. Note that it is not necessary to escape // the string "Joe's Widgets" in any way $tmp = json_encode($tag); $tmp[0] = '{'; $tmp[strlen($tmp) - 1] = '}'; $result = pg_execute($dbconn, "my_query", array($tmp)); 

Вы можете попытаться сериализовать его:

 $tag = array('item1', 'item2', 'item3'); $tag = serialize($tag); // Prepare a query for execution $result = pg_prepare($dbconn, "my_query", "INSERT INTO $table ($column) VALUES ($1)"); // Execute the prepared query. Note that it is not necessary to escape // the string "Joe's Widgets" in any way $result = pg_execute($dbconn, "my_query", $tag); 

Затем, когда вы хотите получить его из БД в качестве массива PHP, выполните его неэтериализацию.