Backup MySQL Database as SQL File with PHP


Backup MySQL Database as SQL File can be done with PHP Programming. It wasn't new thing for the expert. Backup MySQL Database as SQL File can be an alternative programming if something wrong (with server acces Telnet access, or PHPMyadmin). so the better thing we prepare is a backup programming MySQL Database as SQL File with PHP.

A simple way to do this is using a PHP Class to Backup MySQL Database as SQL. But we will got some times to search. Simply use code provide below will help us to do any Backup MySQL Database as SQL File.

The result of the Class is a SQL file with named set in the Class. The SQL File contain all Table as a data definition language (DDL) and data manipulation language (DML).

You need just provide a mysql connection in connection file, example here, i use mysqli_connect

$link=mysqli_connect($server, $username, $password, $database);


<?php
require_once('../include/koneksi.php');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=
". "kkkk". date('Ymd'). ".sql ");
header("Content-Transfer-Encoding: binary ");


echo "create database if not exists `db_wisuda`;\r\n";
echo "\r\n";
echo "use `db_wisuda`;\r\n";
echo "\r\n";
$m = "SHOW TABLE STATUS FROM db_wisuda";
$n = mysqli_query($link,$m);

while ($datao = mysqli_fetch_array($n))
{
$table=$datao[0];
echo "/*Table structure for table `$table` */\r\n";
echo "\r\n";
echo "DROP TABLE IF EXISTS `$table`;\r\n";
echo "\r\n";
echo "CREATE TABLE `$table` (\r\n";
$sql0="desc $table";
$res0= mysqli_query($link,$sql0);
$jum0=mysqli_num_rows($res0);
$j=0;
$key=0;
while($row0= mysqli_fetch_row($res0)){
$j++;
if($jum0==$j and $key==0)
{
if($row0[2]=='NO')
{
echo "`$row0[0]` $row0[1] NOT NULL);\r\n";
}
else
{
echo "`$row0[0]` $row0[1] default NULL);\r\n";
}
}
else
{
if($row0[2]=='NO')
{
echo "`$row0[0]` $row0[1] NOT NULL,\r\n";
}
else
{
echo "`$row0[0]` $row0[1] default NULL,\r\n";
}
}
if($row0[3]=='PRI')
{
$key=$key+1;
}
}
$sql1= "desc $table";
$res1= mysqli_query($link,$sql1);
$k=0;
while ($row1= mysqli_fetch_row($res1))
{
if($row1[3]=='PRI')
{
$k++;
if($key-1==0)
{
echo "PRIMARY KEY (`$row1[0]`)\r\n";
echo ");\r\n";
}
else if($k>1 and $k<$key)
{
echo "`$row1[0]`,";
}
else if($k==$key)
{
echo "`$row1[0]`)\r\n";
echo ");\r\n";
}
else
{
echo "PRIMARY KEY (`$row1[0]`,";
}
}
}
echo "\r\n";
$sql= "SELECT * FROM $table";
$res= mysqli_query($link,$sql);
while($row= mysqli_fetch_row($res)){
echo "INSERT INTO `$table` VALUES('".implode("','",$row)."');\r\n";
}
}
echo "\r\n";
?>




Komentar FB


Probably Related Article



20 comments:

rumah blogger said...
December 23, 2009 at 2:39 AM

wah saya hanya bisa manggut-manggut nich...

sabirinnet said...
December 23, 2009 at 2:40 AM

nice info friend, good look

Novita said...
December 24, 2009 at 2:34 AM

keren infonya sahabatku, ditunggu kunjungan dan komen baleknya ya!

NLE RESULT NOVEMBER 2009 said...
December 24, 2009 at 5:38 PM

Merry X-mas to u and ur family. Take care

Natha Lia said...
December 25, 2009 at 10:00 PM

nice info sob.....tq

See More My Blog

Natha Lia Blog

Download Ebook Free

Download Full Free Software

Adult Blog

reza said...
December 27, 2009 at 11:44 PM

a nice tips.....if u want to back up sql database...you must have web server like AppServ or XAMPP...u can also import it to web server again

Mark Andrews said...
January 2, 2010 at 7:54 PM

Fantastic, I will use this code thank you :-)

webmaster said...
January 3, 2010 at 2:39 AM

This post gives benefit to new users submit your site to my directory http://www.sitepromoter.co.cc

MySQL Code Samples said...
January 3, 2010 at 11:01 AM

Thanks this helped me learn php and mysql

Cellular Mobile said...
January 31, 2010 at 12:42 AM

Since I have completed my professional Computer Degree recently, I am planning to jump into the web development field. I am exploring your blog since long time and I can see that you have lot of material regarding PHP. One who wants the easy learning with PHP can found your blog really helpful. You have shared a great tip for saving the SQL backup in PHP. Thanks for sharing with us.

Home Loans said...
February 17, 2010 at 9:38 PM

PHP is one of the powerful tool for the web applications development. I have just started learning PHP and I can say that if I will continuously keep working with PHP no doubt I will be expert in PHP.

Home Loans said...
February 17, 2010 at 9:38 PM

PHP is one of the powerful tool for the web applications development. I have just started learning PHP and I can say that if I will continuously keep working with PHP no doubt I will be expert in PHP.

Jingles said...
February 17, 2010 at 11:52 PM

In past days I thought to go for the web development with PHP but could not do that due to two reasons, one when I started doing this I could write only two or three applications for the beginners. I was having some problem with my operating system so that I replaced my operating system and then I installed the Apache server which was not working properly and I could not find the solution to this problem and after some days my laptop was dead. But I have heard that PHP is one of the powerful language for creating the dynamic web pages.

Sex Impotence said...
February 18, 2010 at 11:24 AM

I started development with PHP for the few days. But Later I moved towards the Java. I can say that Java is one of the best language to practice and to embed in the electronic devices. I need the Java tutorials for the beginners. If some one could provide me the basic java tutorials would be really beneficial.

Amatory Watches said...
February 19, 2010 at 9:47 PM

PHP is a server-side scripting language for creating dynamic Web pages. You create pages with PHP and HTML. When a visitor opens the page, the server processes the PHP commands and then sends the results to the visitor's browser, just as with ASP or ColdFusion. Unlike ASP or ColdFusion, however, PHP is Open Source and cross-platform. PHP runs on Windows NT and many Unix versions, and it can be built as an Apache module and as a binary that can run as a CGI. When built as an Apache module, PHP is especially lightweight and speedy. Without any process creation overhead, it can return results quickly, but it doesn't require the tuning of mod_perl to keep your server's memory image small.

Blog Literature said...
March 16, 2010 at 1:41 PM

I was searching it for last 3 weeks but i didn't get the correct code yet. I hope this code 'll prove to be very helpful for me. I 'll definitely try it. Thanks for sharing such a nice post.

金瓜米粉Jason said...
May 5, 2010 at 7:25 PM

徵信社,尋人,偵探,偵探社,徵才,私家偵探,徵信,徵信社,徵信公司,抓猴,出軌,背叛,婚姻,劈腿,感情,第三者,婚外情,一夜情,小老婆,外遇,商標,市場調查,公平交易法,抓姦,債務,債務協商,應收帳款,詐欺,離婚,監護權,法律諮詢,法律常識,離婚諮詢,找人,追蹤器,GPS,徵信,徵信公司,尋人,抓姦,外遇,徵信,徵信社,徵信公司,尋人,抓姦,外遇,徵信,徵信社,徵信公司,尋人,抓姦,外遇,徵信,徵信社,徵信公司,尋人,抓姦,外遇,徵信社

Health Insurance said...
August 2, 2010 at 9:39 AM

Nice, Your article helped me learn php programming

Discount loans said...
September 14, 2010 at 3:16 AM

I need the Java tutorials for the beginners. If some one could provide me the basic java tutorials would be really beneficial.

Radio Ads said...
June 3, 2011 at 10:14 AM

Can you still do this on a shared server?

Related Posts by Categories



Widget by