PHP 8.3.4 Released!

pg_put_line

(PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8)

pg_put_lineNULL で終わる文字列を PostgreSQL バックエンドに送信する

説明

pg_put_line(PgSql\Connection $connection = ?, string $data): bool

pg_put_line() は、NULL で終わる文字列を PostgreSQL バックエンドサーバーに送信します。これは、PostgreSQL の COPY FROM コマンドとともに使用する場合に必要となります。

COPY は、PostgreSQL によってサポートされている 高速なデータ読み込みインターフェイスです。データの内容はパースされず、 一度のトランザクションで実行されます。

低レベルな pg_put_line() コマンドを用いない別の方法は、 pg_copy_from() を使用することです。これは、はるかに シンプルなインターフェイスです。

注意:

pg_end_copy() を実行する際には、送信データの最後に 明示的に "\." の 2 文字を送信する必要があります。これによって、 バックエンドに対してデータ送信の終了を通知します。

警告

pg_put_line() の使用は、pg_lo_read()pg_lo_tell() などを含むラージオブジェクトの操作を 発生させ、これが失敗することもあります。そのような場合、かわりに pg_copy_from() および pg_copy_to() が使用可能です。

パラメータ

connection

PgSql\Connection クラスのインスタンス。 connection が指定されない場合は、デフォルトの接続を使います。 デフォルトの接続とは、pg_connect() または pg_pconnect() によって確立された直近の接続です。

警告

PHP 8.1.0 以降では、デフォルトの接続を使うことは推奨されなくなりました。

data

PostgreSQL バックエンドに直接送信されるテキストデータ。 最後に NULL が自動的に付加されます。

戻り値

成功した場合に true を、失敗した場合に false を返します。

変更履歴

バージョン 説明
8.1.0 connection は、PgSql\Connection クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、リソース を期待していました。

例1 pg_put_line() の例

<?php
$conn
= pg_pconnect("dbname=foo");
pg_query($conn, "create table bar (a int4, b char(16), d float8)");
pg_query($conn, "copy bar from stdin");
pg_put_line($conn, "3\thello world\t4.5\n");
pg_put_line($conn, "4\tgoodbye world\t7.11\n");
pg_put_line($conn, "\\.\n");
pg_end_copy($conn);
?>

参考

add a note

User Contributed Notes 2 notes

up
2
kurt at nospam dot milliganshome dot net
18 years ago
This is the function you need if you are running into the infamous "must be superuser to COPY to or from a file" error from postgres.
up
0
smcbride at msn dot com
3 years ago
When using this function, don't get bit by using 'literal\tanotherliteral\n' issue by using single quotes vs. double quotes. "literal\tanotherliteral\n" is not the same. Many of the functions are impacted on how double quotes treats escape characters different than single quotes. I forget it all the time.
To Top