How To Restore MySQL Database From SQL File Using PHP
Hi guys,
Today i will explained How To Restore MySQL Database From SQL File Using PHP. This example is so easy to use in php.
This example to i am import to the sql file in file folder and store your all database table field to your new database in sql file through.
So let's start to the example.
index.php
<?php
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbName = 'db_backup';
$filePath = 'files/nicesnippets.sql';
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$templine = '';
// Read in entire file
$lines = file($filePath);
$error = '';
foreach ($lines as $line){
// Skip it if it's a comment
if(substr($line, 0, 2) == '--' || $line == ''){
continue;
}
$templine .= $line;
if (substr(trim($line), -1, 1) == ';'){
// Perform the query
if(!$db->query($templine)){
$error .= 'Error performing query "<b>' . $templine . '</b>": ' . $db->error . '<br /><br />';
}
$templine = '';
}
}
return !empty($error)?$error:true;
?>
Now you can check your own.
- How to Increase max_connections in MySQL?
- How to Install the PHP PDO MySQL Extension on Ubuntu 22.04?
- How to Install PHP MySQL on Linux Ubuntu?
- How to Install LEMP Stack Nginx, MySQL, PHP on Ubuntu?
- How to Change MySQL Root User Password Ubuntu?
- PHP 8 MySQL Form Validation Example
- How to Select First Row Only in PHP MySQL?
- How To Backup MySQL Database Using PHP Example