Contents /
Previous /
Next
Copy
DB-Table -> PHP-Array
pg_copy_to copies a table to an array:
array pg_copy_to ( resource connection,
string table_name
[, string delimiter
[, string null_as]] )
It issues the "COPY TO" SQL command internally to insert records.
The resulting array is returned or FALSE on failure.
Example:
$con = "dbname=workshop user=felix";
$dbh = pg_connect( $con );
$result_rows = array();
if( $dbh )
{
$result_rows = pg_copy_to ( $dbh, "stud" );
if ( ! $result_rows ) { echo "No result!
"; return; }
foreach( $result_rows as $row ) {
echo $row."
";
}
}
else
{
echo "connection error
\n";
}
// Output:
// 1 fred 2 bio m
// 3 tom 1 bio m
// 2 lisa 2 bio f
// 5 BoB \N bio f
?>
PHP-Array -> DB-Table
pg_copy_from inserts records into a table from an array:
bool pg_copy_from ( resource connection,
string table_name,
array rows
[, string delimiter
[, string null_as]] )
The table "table_name" must exist in the DB.
It issues the "COPY FROM" SQL command internally to insert records.
It returns TRUE on success or FALSE on failure.
Example:
";
$result_rows = pg_copy_to ( $dbh, "stud_test" );
if ( ! $result_rows ) { echo "No result!
"; return; }
echo "// stud_test:
";
foreach( $result_rows as $row ) {
echo "// ".$row."
";
}
$query_str= "DROP TABLE stud_test;";
$res = pg_query( $dbh, $query_str );
// Output:
// copy status: 1
// stud_test:
// 1 fred 2 bio m
// 3 tom 1 bio m
// 2 lisa 2 bio f
// 5 BoB \N bio f
?>