note 87511 added to ref.pgsql

From: Date: Mon, 08 Dec 2008 09:07:37 +0000
Subject: note 87511 added to ref.pgsql
Groups: php.notes 
Request: Send a blank email to php-notes+get-147906@lists.php.net to get a copy of this message
<?php function pg_export_table_content() { /* * this function exports all contents of all tables in the file import.sql * $pg-> = my postgres db class */ $tables = array(); $mytables = array(); $all_tables = $pg->query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' and table_type = 'BASE TABLE';"); while($row = $pg->fetch_row($all_tables)) { $tables[] = $row->table_name; } $esc[] = "value"; $esc[] = "time"; $esc[] = "order"; $esc[] = "comment"; $esc[] = "group"; $esc[] = "time"; $esc[] = "name"; $esc[] = "alter"; $esc[] = "user"; foreach($tables as $v1) { $myqry = "select * from \"".$v1."\""; $myres = $pg->query($myqry); while($myrow = $pg->fetch_assoc($myres)) { $mytables[$v1][] = $myrow; } } $handle = fopen("./import.sql", "w+"); foreach($mytables as $k1 => $v1) { foreach($v1 as $k2 => $v2) { foreach($v2 as $k3 => $v3) { if(in_array($k3, $esc)) { $mycols[] = "\"".$k3."\""; } else { $mycols[] = $k3; } $col_qry = "select column_name, data_type from information_schema.columns where table_name = '".$k1."'"; $col_res = $pg->query($col_qry); while($col_row = $pg->fetch_assoc($col_res)) { $mydatatypes[$col_row["column_name"]] = $col_row["data_type"]; } if(($mydatatypes[$k3] == "integer") || ($mydatatypes[$k3] == "bigint") || ($mydatatypes[$k3] == "smallint")) { if(empty($v3)) { $myval[] = "NULL"; } else { $myval[] = $v3; } } else { $myval[] = "'".$v3."'"; } } $myquery = "insert into ".$k1."(".implode(",", $mycols).") VALUES (".implode(",", $myval).");"; unset($myval,$mycols); fwrite($handle, $myquery."\n"); } } fclose($handle); } ?> ---- Server IP: 212.124.37.9 Probable Submitter: 87.139.20.236 ---- Manual Page -- http://www.php.net/manual/en/ref.pgsql.php Edit -- https://master.php.net/note/edit/87511 Del: integrated -- https://master.php.net/note/delete/87511/integrated Del: useless -- https://master.php.net/note/delete/87511/useless Del: bad code -- https://master.php.net/note/delete/87511/bad+code Del: spam -- https://master.php.net/note/delete/87511/spam Del: non-english -- https://master.php.net/note/delete/87511/non-english Del: in docs -- https://master.php.net/note/delete/87511/in+docs Del: other reasons-- https://master.php.net/note/delete/87511 Reject -- https://master.php.net/note/reject/87511 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#147906) next »