How to Export and Import MySQL Database with Its Data Using PHP Script

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

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);

How do I import a .sql file in mysql database using PHP?

Warning: mysql_* extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

Whenever possible, importing a file to MySQL should be delegated to MySQL client.

I have got another way to do this, try this

<?php

// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dump';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>

This is working for me

how to import a .sql file using php function

It's working now!
Firstly, it didn't work in local server and secondly, instead of exec() function I used the system() function and finally, it generates a table in my database. Thanks everyone!

Export/Import some data from database with PHP

Try not to use mysqldump (it is more like an administration/backup tool and to my knowledge can only export whole tables), but try a SELECT statement with a WHERE clause for reading your data. Export it in the file format you would like to, e.g. XML oder CSV. For importing the data again, use a DELETE statement for deleting the present data (also applying the WHERE clause) and use INSERT statements for each record in the input file.

Create MySQL DB export via PHP

$dump = shell_exec('mysqldump --user=user --password=asdasd --host=localhost db_name');

alternatively you can use php implementation of mysqldump, available on https://github.com/clouddueling/mysqldump-php



Related Topics



Leave a reply



Submit