Backup Data from Database using SQL Command through PHP

Backup Data from Database using SQL Command through PHP

Hello everyone! Today, I’m gonna teach you How to Backup MySQL Database in PHP.

This tutorial can answer the question of this article on how to backup MySQL Database in PHP SCRIPT.

There are three (3) ways of taking a backup of your MySQL databases which are: using SQL Command, using MySQL Binary mysqldump, these two are through PHP, and last is using phpMyAdmin user interface.

But for now, we will use the first way.

so let’s start.

First, execute a SQL SELECT command to take a backup of any table and to complete database dump you must need to write a separate query for a separate table and each table will be stored into the separate text file.

And the codes for that are as follows:

<?php
include 'conn.php';
$table_name = "files";
$backup_file = 'db_backup/files.sql';
$backup_db = $conn->query("SELECT * INTO OUTFILE '$backup_file' FROM $table_name");
if (!$backup_db) { 
die('Could not take data backup: ' . $conn->error);
} else { 
echo 'Backed up data successfully!'; 
}
?>

 

And that’s all the codes needed for backing up of data in using the SQL command through PHP. That’s all.

Inquiries

If you have questions regarding these tutorial entitled as “Backup Data from Database using SQL Command through PHP” feel free to ask us by commenting below or visit on our contact page. Thank you.

For other great mini projects in PHP, click here and for other great database examples check out the list of mini-projects for 2019.

Leave a Comment