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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.