Jump to content

backup of mysql using php...


antonyjohn

Recommended Posts

$db_host = "localhost";
$db_name = "dinasteel";
$db_user = "root";
$db_pass = "zenith2255";

mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name) or die("Unable to select database.");function datadump ($table) 
{

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

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

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

$content = $dn_country . $dn_payments . $dn_projecttracking;

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

 

 

i have written this program to backup my database....the file is been downloaded....but with some problem

 

when i open the text file ....the written like this.....INSERT INTO dn_country VALUES("","","");

 

 

plzzzzzzzz help ...ver is the data

 

Link to comment
https://forums.phpfreaks.com/topic/78529-backup-of-mysql-using-php/
Share on other sites

<?php

$db_host = "localhost";
$db_name = "dinasteel";
$db_user = "root";
$db_pass = "zenith2255";

mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name) or die("Unable to select database.");function datadump ($table) 
{

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

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

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

$content = $dn_country . $dn_payments . $dn_projecttracking;

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

?>

 

 

i have written this program to backup my database....the file is been downloaded....but with some problem

 

when i open the text file ....the written like this.....INSERT INTO dn_country VALUES("","","");

 

 

plzzzzzzzz help ...ver is the data

 

 

I'm assuming 'ver' means 'where'...

 

For a start change your query - you have:

 

<?php

$query = mysql_query("select * from $table");

?>

 

Change that to:

 

<?php

$sql = "SELECT * FROM $table"; //No where in your code is $table defined!
$query = mysql_query($sql) or die(mysql_error);

?>

 

There is (should be) a feature in  phpMyAdmin to back up your tables or database (at least on my web-host there is) - you could always just use that. Might be simpler and would probably work.

if I remember correctly those function are there in a function file in phpMyAdmin.. A long time ago I tweak them for a backup system for a site I did, just search the phpMyAdmin directory the functions are pretty easy to change to your needs

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.