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!<BR>"; return; } foreach( $result_rows as $row ) { echo $row."<BR>"; } } else { echo "connection error<BR>\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:

<?php $con = "dbname=workshop user=felix"; $dbh = pg_connect( $con ); $query_str= "SELECT * INTO stud_test FROM stud;"; $res = pg_query( $dbh, $query_str ); $query_str= "DELETE FROM stud_test;"; $res = pg_query( $dbh, $query_str ); $status = pg_copy_from ( $dbh, "stud_test", $result_rows ); echo "copy status: ".$status."<P>"; $result_rows = pg_copy_to ( $dbh, "stud_test" ); if ( ! $result_rows ) { echo "No result!<BR>"; return; } echo "// stud_test:<BR>"; foreach( $result_rows as $row ) { echo "// ".$row."<BR>"; } $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 ?>