Export and Import MySQL database using PHP

I need to import and export mysql databases using php code, as a part of backing up and restoring database in the admin panel of the web site.
Someone help me out.
Need it soon.

lots of ways to do it… one way I’ve done it when dealing with large sql files is open a pipe to mysqladmin and dump the SQL into it.

 class Process
     private $descriptorspec;
     private $process;
     private $pipes;

     public function __construct()
         $this->descriptorspec = array(
                    0 => array("pipe", "r"),
                    1 => array("pipe", "w"),
                    2 => array("file", "/tmp/error-output.txt", "a")
     public function connect($command)
         $this->process = proc_open($command,
         if (!is_resource($this->process))
             return false;

     public function send($message)
         fwrite($this->pipes[0], $message);

     public function read()
            while (!feof($this->pipes[1]))
                $response .= fgets($this->pipes[1], 1024);


         return $response;

     public function close()
            return proc_close($this->process);
     public function __destruct()

/********** usage *****/
 $process =& new Process;
 $process->connect('mysqldump -u testuser -p --no-data testdb');
 $response = $process->read();

 echo nl2br($response);

this works… however the process class is “funny” as I cannot seem to read or wite to a proces after the initial read/writes. Still… works for the task at hand (you could replace the command with a mysql command… i.e. ‘mysql -u user -p db < sqlfile.sql’ and write your password to it. if you don’t have pcntl, you can use popen (www.php.net/popen).

Also, in the most barebones of ways, you could read from the sql file line by line and pass it to mysql_query. :slight_smile:

Thank you for the advice,
I tried it but I need the code that runs in the windows platform. Unix specific functions such as proc_open() could not be executed.
So please help me out.

Moved to a more appropriate forum. See Where should I post my thread? for details

Sean :slight_smile:

i use phpmyadmin

or better yet, call mysql’s mysqldump to have mysql do all the work…

a single line of php code will backup the stuff…