How to export and import mysql database with its data using php script?

前端 未结 3 1205
鱼传尺愫
鱼传尺愫 2020-12-04 19:09

I was asked to create an export and import mysql database with it\'s structure and data into a .sql file using php scripting and not phpmyadmin to make the user be able to b

相关标签:
3条回答
  • 2020-12-04 19:10

    http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx

    either

    $tableName  = 'mypet';
    $backupFile = 'backup/mypet.sql';
    $query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
    $result = mysql_query($query);
    

    or

    $backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
    $command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip >     $backupFile";
    system($command);
    
    0 讨论(0)
  • 2020-12-04 19:12

    SAFE and WORKING SOLUTION :

    EXPORT_TABLES("localhost","user","pass","db_name"); 
    // has 5th and 6th optional parameters too.
    

    Latest version is available at github: Export.php + Import.php

    0 讨论(0)
  • 2020-12-04 19:15

    Here is a nice class that worked just fine for me. if it says fopen access denied, try to change the folder permissions.

    https://github.com/clouddueling/mysqldump-php

    0 讨论(0)
提交回复
热议问题