How can I import MySQL database using PHP? Also if you have to import large size of MySQL dump, how do you import it easily? In this article is describing how to import MySQL database using PHP.In this method you can save your time instead of you import sql file in the phpmyadmin or database software.As my personal experience I tested this script using 2.125GB large database dump file.
1-First create a database and user. Assign user to the database.
2-Copy and paste the following php scripts and insert your database,users details to the variables.
error_reporting(E_ALL ^ E_NOTICE); @include_once('phpMyImporterClass.php'); $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "demo_db"; $filename = "database_dump.sql"; // Filename of dump, default: dump.sql $compress = false; // Import gz compressed file, default: false $connection = @mysql_connect($dbhost,$dbuser,$dbpass); $dump = new phpMyImporter($dbname,$connection,$filename,$compress); $dump->utf8 = true; // Uses UTF8 connection with MySQL server, default: true $dump->doImport(); ?>
3-Keep the database dump file in to the same directory.
4-Also keep the phpMyImporterClass.php file.
5-Run the import.php file.
Note:
If you run the large size of database file, remember increase the max_execution_time and memory_limit values in the server(If you are running on the local server change it in the php.ini file and restart the server).
That’s only.After execution the result will be displayed on the import.php file.
How can I import zip or gz format file?
Simply change the code like this.
$filename = "database_dump.gz"; $compress = true;
Download Import a MySQL database using PHP Example(4 KB)