Free Webmasters Resources, Webmaster Tools, CGI Scripts, Web Tutorials, Web Articles  

Dating Services Software
Navigation
Home
Advertise
Webmaster Forums
Webmaster Resources
Webmaster Tools
Web Articles
Tutorials
Free Newsletter

Link To Us
Site Map
About Us
Search
Contact

Webmaster Resources
Resource Home
Submit Resource

Commerce
Free Services
Learning
Online Tools
Our Webmaster Tools
Promotion
Scripts
Software
Web Hosting
Website Design


Webmaster Tools
SEO Tools

- Meta Tag Generator
- Google Position Checker
- Keyword Density Analyzer
- Keyword Generator
- Link Popularity Checker
- Meta Tag Analyzer
- Search Index Report
- Search Engine Simulator
- Web Page Analyzer
- Yahoo Position Checker

HTML Tools

- Color Chart
- Color Converter
- Dreamweaver Code Cleaner
- Frontpage Code Cleaner
- HTML Entities List
- Web Page Size Checker

Domain Tools

- Domain Typo Generator
- Whois Lookup

Other Tools

- HTTP Headers Viewer
- IP Address


Free Newsletter
This is a private mailing list and will never be sold or given away for any reason. Newsletter sent out in HTML format.


First Name:


Email Address:




More Information
Archives


Poll
Where do you live?
Africa
North America
South America
Asia
Australia/Pacific
Europe
 

Results
Past Results


Site Search
Search this entire Website for articles, tutorials, reviews and other resources.


Adv. Search

Scripts & Programs
Submit Script

ASP Scripts
C and C++ Scripts
JavaScripts
Perl and CGI Scripts
PHP Scripts
Remotely Hosted


Web Articles
Submit Article

Coding
eZine Production
Marketing Tips
Online Business
Other Internet Articles
Search Engines
Web Design
Web Hosting


Tutorials
Submit Tutorial

ASP Tutorials
C and C++ Tutorials
ColdFusion Tutorials
DHTML Tutorials
Flash Tutorials
Graphic Tutorials
HTML Tutorials
JavaScript Tutorials
Perl Tutorials
PHP Tutorials
Web Design Tutorials
XML Tutorials


How To Backup Your MySQL Database With PHP

Home Page :: Authors Login :: Author Signup :: Search :: More Articles

Home :: PHP/MySQL


About This Author :: Request Reprint :: Print Article :: Tell A Friend


How To Backup Your MySQL Database With PHP
By Ben Sinclair
Contact Ben Sinclair


If your website some how gets hacked and you lose all your MySQL Database information, I doubt you would be laughing... or smiling... That's why you need to backup your database from time to time so that you can put it back the way it was. Here is a simple script using PHP and MySQL to backup your database!

The Script

First of all you need to connect to the database:


<?php
$db_host
= "localhost";
$db_name = "";
$db_user = "";
$db_pass = "";

mysql_connect($db_host,$db_user,$db_pass);
@
mysql_select_db($db_name) or die("Unable to select database.");
?>



And then you need to create a function that will backup your table contents:


<?php
function datadump ($table) {

&
nbsp; &nbsp; $result .= "# Dump of $table \n";
&
nbsp; &nbsp; $result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";

&
nbsp; &nbsp; $query = mysql_query("select * from $table");
&
nbsp; &nbsp; $num_fields = @mysql_num_fields($query);
&
nbsp; &nbsp; $numrow = mysql_num_rows($query);

&
nbsp; &nbsp; for ($i =0; $i<$numrow; $i++) {
&
nbsp; &nbsp;&nbsp; &nbsp;$result .= "INSERT INTO ".$table." VALUES(";
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for($j=0; $j<$num_fields; $j++) {
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $row[$j] = addslashes($row[$j]);
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; $row[$j] = ereg_replace("\n","\\n",$row[$j]);
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if ($j<($num_fields-1)) $result .= ",";
&
nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;
&
nbsp; &nbsp; &nbsp; $result .= ");\n";
&
nbsp; &nbsp; &nbsp;}
&
nbsp; &nbsp; &nbsp;return $result . "\n\n\n";
&
nbsp; }
?>



OK, now you add the last bit to the script:


<?php
$table1
= datadump ("table1");
$table2 = datadump ("table2");
$table3 = datadump ("table3");

$content = $table1 . $table2 . $table3;

$file_name = "MySQL_Database_Backup.sql";
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$file_name");
echo
$content;
exit;
?>



See this part:


$table1 = datadump ("table1");
$table2 = datadump ("table2");
$table3 = datadump ("table3");

$content = $table1 . $table2 . $table3;



That is where you need to specify the tables you want to backup.

Lets say you had 3 tables called links, categories and users. You would need to add:


$links = datadump ("links");
$categories = datadump ("categories");
$users = datadump ("users");



And then to the $content area add:


$content = $links . $categories . $users;



That's it. Enter your MySQL Database information and fill in the tables an hey presto, run the script and it should backup your table contents into a .sql file for download!

If your database information every got lost, you have a backup :)

Enjoy!

Discuss this in our Webmaster Forums

About The Author:
Ben Sinclair

Ben Sinclair is the Webmaster of Webmaster Resources 101(/)

Go To Top

This article has been read 5 times.


About This Author :: Request Reprint :: Print Article :: Tell A Friend



Advertising
Advertise your website here. View Advertising Information.

Google Ranking Tool
Search Engine OptimizationGet This Tool Free!

Home   Search   Contact   About Us  

Hot Resources
We have found some of the Hottest Resources around just for you! Check these out:
Search Engine Promotion
- Search engine specialist
- Over 200 clients worldwide
- SEO as low as $499
- Link Building for $150

More info...

Online Dating Script

aeDating 3.0 new version of famous dating software with free installation, lifetime upgrades and technical support. New templates, private photos, credits payment system, integrated with phpBB, banner system, new IM.

Visit resource

Search Engine Placement
Since 1997 StepForth has been a result and client oriented search engine optimization and placement company. Our SEO services are guaranteed and proven. Questions? Contact us: 1-877-385-5526

More info...

Best Web Hosting!
- LunarPages ($7.95)
- iPowerWeb ($7.95)
- Globat ($7.50)
- PowWeb ($7.77)

WebHosting $7.50/mo
$7.50/mo - 1000 MB Space, 40 GB Transfer, 24/7 Support, 99.9% Uptime, FREE Domain and FREE Setup.

More info...


Sponsors
Web Hosting
Web Hosting India
StartLogic
Web Hosting
Cheap Web Hosting

Search Engines
Search Engine Optimization SEO
NetVisits Web Directory
Search Engine Optimization

Webmaster Tools
Webmaster Resources

Software
MAS 90

Domain Names
Resell Domain Names and More!

Link Popularity
Reciprocal Link Exchange Directory
Link Building Campaigns

Great Deals
Cheap Flights
Cheap Car Rental
Cheap Mobile Phones
Plasma Televisions



Partners
Dreamweaver Web Templates
Motivational Memo
Website Advertising
Php Scripts
Cheap Web Hosting
Cheap Web Host
Free Webmaster Resources
Need CGI
Desro Webmaster Resources
Web-Mastery Web-Resources
Top 10 Hosts - PHP MySQL

Books
HTML For Dummies�

HTML For Dummies�


The Hottest Reseller Program Going!

Contact Information

Powered by Outasight Enterprises Pty Ltd
PO Box 1218 Mudgeeraba QLD 4213 AUS
All content � Webmaster Resources 101, 2002-2004 unless otherwise stated.
Design by OE Design Hosted by Surf Speedy View our Privacy Policy
Page loaded in 2.193 secs.
8 users online.

The Hottest Reseller Program Going!