Hi Guys! I have here a PHP Source Code that you might need for your Web Application Developments. This is a simple Database Backup System for your web applications. This Simple Web Application has a simple form where you will input all necessary information need from your database including the database you wanted to back up. The purpose of this article is to help you guys especially those beginners in developing a Web Application using PHP Language.
The code that I have created for this article has a simple user-interface that has a single form for dynamically configuring/input the database credential of your wanted database to be backed up. The backup file will be automatically downloaded as an SQL file. The Code that I have used for the backup database function is the script below.
<?php function backDb($host, $user, $pass, $dbname, $tables = '*'){ $conn = new mysqli($host, $user, $pass, $dbname); if ($conn->connect_error) { } if($tables == '*'){ $sql = "SHOW TABLES"; $query = $conn->query($sql); while($row = $query->fetch_row()){ $tables[] = $row[0]; } } else{ } $outsql = ''; foreach ($tables as $table) { $sql = "SHOW CREATE TABLE $table"; $query = $conn->query($sql); $row = $query->fetch_row(); $outsql .= "\n\n" . $row[1] . ";\n\n"; $sql = "SELECT * FROM $table"; $query = $conn->query($sql); $columnCount = $query->field_count; for ($i = 0; $i < $columnCount; $i ++) { while ($row = $query->fetch_row()) { $outsql .= "INSERT INTO $table VALUES("; for ($j = 0; $j < $columnCount; $j ++) { $row[$j] = $row[$j]; $outsql .= '"' . $row[$j] . '"'; } else { $outsql .= '""'; } if ($j < ($columnCount - 1)) { $outsql .= ','; } } $outsql .= ");\n"; } } $outsql .= "\n"; } } ?>
The backDb above will be the function that will call when backing up a database.
Example
Assuming that the script above is written inside the "db_bup_function.php" file.
<?php // include first the function in your other file include "db_bup_function.php"; //Database Credentials Vairables $servername = "localhost"; $username = "root"; $password = "MyDbPassword"; $db_name = "MyDB"; // Initiating the backup database function backDb($servername, $username, $password, $db_name); ?>
The above example will backup all the tables in your given database. And to backup only a selected tables you can use the scripts below.
Backup a single table only
<?php backDb($servername, $username, $password, $db_name,'table1'); ?>
Backup a selected multiple tables
<?php backDb($servername, $username, $password, $db_name,); ?>
To try the source code provided follow the instruction below.
I hope this simple source code will help you with what you are looking for and also for your future PHP Projects. Feel free to modify the source code in the way you wanted.
Enjoy :)Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.