LonghornPHP 2026

PDO::pgsqlCopyFromArray

(PHP 5 >= 5.3.3, PHP 7, PHP 8)

PDO::pgsqlCopyFromArray Alias de Pdo\Pgsql::copyFromArray()

Advertencia

Esta función está OBSOLETA a partir de PHP 8.5.0. Depender de esta función está altamente desaconsejado.

Descripción

#[\Deprecated]
public function PDO::pgsqlCopyFromArray(
string $tableName,
array |Traversable $rows,
string $separator = "\t",
string $nullAs = "\\\\N",
? string $fields = null
): bool

Este método es un alias de: Pdo\Pgsql::copyFromArray() .

Historial de cambios

Versión Descripción
8.5.0 El parámetro rows ahora también acepta un Traversable ; anteriormente sólo se aceptaba un array .

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 1 note

up
1
Anonymous
10 years ago
If your $nullAs is '\\N', then you should use $nullAs as is in concatenation of cells of $rows, but send to pgsqlCopyFromArray() escaped version. Also fifth arg $fields should be a SQL-valid string for the column_names placeholder in COPY statement of PostgreSQL.
I provide my smart wrapper for pgsqlCopyFromArray() which do this automatically.
<?php
/**
 *
 * @param PDO $db
 * @param string $tableName
 * @param string[] $fields List of fields names.
 * @param array[] $records Two-demension array of cells (array of rows).
 * @return boolean
 */
function pgInsertByCopy (PDO $db, $tableName, array $fields, array $records) {
 static $delimiter = "\t", $nullAs = '\\N';
 $rows = [];
 foreach ($records as $record) {
 $record = array_map(
 function ($field) use( $record, $delimiter, $nullAs) {
 $value = array_key_exists($field, $record) ? $record[$field] : null;
 if (is_null($value)) {
 $value = $nullAs;
 } elseif (is_bool($value)) {
 $value = $value ? 't' : 'f';
 }
 $value = str_replace($delimiter, ' ', $value);
 // Convert multiline text to one line.
 $value = addcslashes($value, "0円..37円");
 return $value;
 }, $fields);
 $rows[] = implode($delimiter, $record) . "\n";
 }
 return $db->pgsqlCopyFromArray($tableName, $rows, $delimiter, addslashes($nullAs), implode(',', $fields));
}
?>
+add a note

AltStyle によって変換されたページ (->オリジナル) /