WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Postgresql Database Backup And Restore PHP script
Categories : PHP, Databases, PostgreSQL Click here to Update Your Picture
ivan valadares
Date : Dec 29th 2005
Grade : 5 of 5 (graded 1 times)
Viewed : 6627
File : 4282.php
Images : No Images for this code example.
Search : More code by ivan valadares
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
// scrypt for backup and restore postgres database


function dl_file($file){
   if (!
is_file($file)) { die("<b>404 File not found!</b>"); }
   
$len = filesize($file);
   
$filename = basename($file);
   
$file_extension = strtolower(substr(strrchr($filename,"."),1));
   
$ctype="application/force-download";
   
header("Pragma: public");
   
header("Expires: 0");
   
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   
header("Cache-Control: public");
   
header("Content-Description: File Transfer");
   
header("Content-Type: $ctype");
   
$header="Content-Disposition: attachment; filename=".$filename.";";
   
header($header );
   
header("Content-Transfer-Encoding: binary");
   
header("Content-Length: ".$len);
   @
readfile($file);
   exit;
}



$action  = $_POST["actionButton"];
$ficheiro=$_FILES["path"]["name"];
switch (
$action) {
    case
"Import":
     
$dbname = "teste"; //database name
     
$dbconn = pg_pconnect("host=localhost port=5432 dbname=$dbname user=postgres password=rularv"); //connectionstring
     
if (!$dbconn) {
        echo
"Can't connect.\n";
        exit;
      }
     
$back = fopen($ficheiro,"r");
     
$contents = fread($back, filesize($ficheiro));
     
$res = pg_query(utf8_encode($contents));
      echo
"Upload Ok";
     
fclose($back);
  break;
  case
"Export":
 
$dbname = "MiradouroTest"; //database name
 
$dbconn = pg_pconnect("host=localhost port=5432 dbname=$dbname user=postgres password=rularv"); //connectionstring
 
if (!$dbconn) {
    echo
"Can't connect.\n";
  exit;
  }
 
$back = fopen("$dbname.sql","w");
 
$res = pg_query(" select relname as tablename
                    from pg_class where relkind in ('r')
                    and relname not like 'pg_%' and relname not like 'sql_%' order by tablename"
);
 
$str="";
  while(
$row = pg_fetch_row($res))
  {
   
$table = $row[0];
   
$str .= "\n--\n";
   
$str .= "-- Estrutura da tabela '$table'";
   
$str .= "\n--\n";
   
$str .= "\nDROP TABLE $table CASCADE;";
   
$str .= "\nCREATE TABLE $table (";
   
$res2 = pg_query("
    SELECT  attnum,attname , typname , atttypmod-4 , attnotnull ,atthasdef ,adsrc AS def
    FROM pg_attribute, pg_class, pg_type, pg_attrdef WHERE pg_class.oid=attrelid
    AND pg_type.oid=atttypid AND attnum>0 AND pg_class.oid=adrelid AND adnum=attnum
    AND atthasdef='t' AND lower(relname)='$table' UNION
    SELECT attnum,attname , typname , atttypmod-4 , attnotnull , atthasdef ,'' AS def
    FROM pg_attribute, pg_class, pg_type WHERE pg_class.oid=attrelid
    AND pg_type.oid=atttypid AND attnum>0 AND atthasdef='f' AND lower(relname)='$table' "
);                                             
    while(
$r = pg_fetch_row($res2))
    {
   
$str .= "\n" . $r[1]. " " . $r[2];
     if (
$r[2]=="varchar")
    {
   
$str .= "(".$r[3] .")";
    }
    if (
$r[4]=="t")
    {
   
$str .= " NOT NULL";
    }
    if (
$r[5]=="t")
    {
   
$str .= " DEFAULT ".$r[6];
    }
   
$str .= ",";
    }
   
$str=rtrim($str, ","); 
   
$str .= "\n);\n";
   
$str .= "\n--\n";
   
$str .= "-- Creating data for '$table'";
   
$str .= "\n--\n\n";

   
   
$res3 = pg_query("SELECT * FROM $table");
    while(
$r = pg_fetch_row($res3))
    {
     
$sql = "INSERT INTO $table VALUES ('";
     
$sql .= utf8_decode(implode("','",$r));
     
$sql .= "');";
     
$str = str_replace("''","NULL",$str);
     
$str .= $sql
     
$str .= "\n";
    }
   
     
$res1 = pg_query("SELECT pg_index.indisprimary,
            pg_catalog.pg_get_indexdef(pg_index.indexrelid)
        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
            pg_catalog.pg_index AS pg_index
        WHERE c.relname = '$table'
            AND c.oid = pg_index.indrelid
            AND pg_index.indexrelid = c2.oid
            AND pg_index.indisprimary"
);
    while(
$r = pg_fetch_row($res1))
    {
   
$str .= "\n\n--\n";
   
$str .= "-- Creating index for '$table'";
   
$str .= "\n--\n\n";
   
$t = str_replace("CREATE UNIQUE INDEX", "", $r[1]);
   
$t = str_replace("USING btree", "|", $t);
   
// Next Line Can be improved!!!
   
$t = str_replace("ON", "|", $t);
   
$Temparray = explode("|", $t);
   
$str .= "ALTER TABLE ONLY ". $Temparray[1] . " ADD CONSTRAINT " . $Temparray[0] . " PRIMARY KEY " . $Temparray[2] .";\n";
    }   
  }
 
$res = pg_query(" SELECT
  cl.relname AS tabela,ct.conname,
   pg_get_constraintdef(ct.oid)
   FROM pg_catalog.pg_attribute a
   JOIN pg_catalog.pg_class cl ON (a.attrelid = cl.oid AND cl.relkind = 'r')
   JOIN pg_catalog.pg_namespace n ON (n.oid = cl.relnamespace)
   JOIN pg_catalog.pg_constraint ct ON (a.attrelid = ct.conrelid AND
   ct.confrelid != 0 AND ct.conkey[1] = a.attnum)
   JOIN pg_catalog.pg_class clf ON (ct.confrelid = clf.oid AND clf.relkind = 'r')
   JOIN pg_catalog.pg_namespace nf ON (nf.oid = clf.relnamespace)
   JOIN pg_catalog.pg_attribute af ON (af.attrelid = ct.confrelid AND
   af.attnum = ct.confkey[1]) order by cl.relname "
);
  while(
$row = pg_fetch_row($res))
  {
   
$str .= "\n\n--\n";
   
$str .= "-- Creating relacionships for '".$row[0]."'";
   
$str .= "\n--\n\n";
   
$str .= "ALTER TABLE ONLY ".$row[0] . " ADD CONSTRAINT " . $row[1] . " " . $row[2] . ";";
  }       
 
fwrite($back,$str);
 
fclose($back);
 
dl_file("$dbname.sql");
  break;
}

?>
 
<html>
<head>
</head>
<body>
<form id="dataForm" name="dataForm" method="post" enctype="multipart/form-data" action="">
    <input type="file" name="path" id="path" style="width:300px"/>
    <input type="submit" value="Import" name="actionButton" id="actionButton" >
    <input type="submit" value="Export" name="actionButton" id="actionButton" >
</form>
</body>
</html>



PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
Script for postgresql to walk through the results limiting the results shown per page.
Categories : PostgreSQL, Databases, PHP
Monthly and Daily Upcoming Events calendar.
Categories : Date Time, PostgreSQL, PHP, Calendar, Databases
Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0.
Categories : Databases, PHP, mSQL, Databases
AUTH (.htaccess style) - a login system that uses PostgreSQL.
Categories : PHP, Authentication, Databases, PostgreSQL
DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser!
Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Is there any way to test that the $result has null values or not without reading the field values in the results in postgre?
Categories : PostgreSQL, PHP, Databases
Save and restore files into postgresql database (PHP SCRIPT) PHP CLASS
Categories : PHP, Databases, PostgreSQL, Filesystem
This is a database wrapper for PostgreSQL, but can be simply modified for any other database type.
Categories : Databases, PostgreSQL, PHP
This is a function to display a table for Postgres results. This this code in an include file, require it, and call ShowResults($result) whenever you need to see what your queries result in.
Categories : Databases, PHP, PostgreSQL
This is Yet Another Sql Abstraction Library. Include it in your script and you can use the most important SQL functions without worrying about the SQL backend.
Categories : Databases, PHP, ODBC, MySQL, PostgreSQL
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
 hedi mettali wrote :1652
Hello;

I test this script it work but have same bug..

The problem :: 

The script cant create the sequences for the autoincrement primary key...