SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast sangharsha's Avatar
    Join Date
    Mar 2005
    Location
    Kathmandu, Oklahoma, New Delhi
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up 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.

  2. #2
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:

     
    class Process
         
    {
         private 
    $descriptorspec;
         private 
    $process;
         private 
    $pipes;
      
         public function 
    __construct()
             {
             
    $this->descriptorspec = array(
                        
    => array("pipe""r"), 
                        
    => array("pipe""w"), 
                        
    => array("file""/tmp/error-output.txt""a")
                 );
             }
         public function 
    connect($command)
             {
             
    $this->process proc_open($command,
                                        
    $this->descriptorspec,
                                        
    $this->pipes);
             if (!
    is_resource($this->process))
                 return 
    false;
             }
        
         public function 
    send($message)
             {
             
    fwrite($this->pipes[0], $message);
             
    fflush($this->pipes[0]);
             }
      
         public function 
    read()
             {   
                while (!
    feof($this->pipes[1]))
                    
    $response .= fgets($this->pipes[1], 1024);
            
             
    fflush($this->pipes[1]);
      
             return 
    $response;
             }
      
         public function 
    close()
             {
             
    fclose($this->pipes[0]);
             
    fclose($this->pipes[1]);
                return 
    proc_close($this->process);
             }
         public function 
    __destruct()
             {
             unset(
    $this->descriptorspec);
             }
     }



    /********** usage *****/
     
    $process =& new Process;
     
    $process->connect('mysqldump -u testuser -p --no-data testdb');
     
    $process->write('test');
     
    $response $process->read();
     
    $process->close();
      
     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.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  3. #3
    SitePoint Enthusiast sangharsha's Avatar
    Join Date
    Mar 2005
    Location
    Kathmandu, Oklahoma, New Delhi
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you but i need it in windows system

    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.

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to a more appropriate forum. See Where should I post my thread? for details

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or better yet, call mysql's mysqldump to have mysql do all the work...

    a single line of php code will backup the stuff...
    leo d.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •