Jump to content

Backup Db Code


son
 Share

Recommended Posts

I try to backup my database with php. But also it all first seems to run fine, it returns an empty result set when I try to import the SQL file. The code is:

<?phprequire_once ('/path/to/file/connect.php'); // connect to databasefunction datadump ($table) {    $result .= "# Dump of $table \n";    $result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";    $query = mysqli_query("select * from $table");    $num_fields = @mysqli_num_fields($query);    $numrow = mysqli_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";  }$table1 = datadump ("content1");$table2 = datadump ("content2");$table3 = datadump ("content3");$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;?>

Can anyone see what is wrong in my code?Son

Link to comment
Share on other sites

A series of insert statements shouldn't return a result set, only a select statement returns a result set. You will want to reset the value of $result every time through the function though, you never initialize that variable you just start adding to it.

Link to comment
Share on other sites

A series of insert statements shouldn't return a result set, only a select statement returns a result set. You will want to reset the value of $result every time through the function though, you never initialize that variable you just start adding to it.
I got your first point (A series of insert statements shouldn't return a result set, only a select statement returns a result set. ), but am lost in what you mean by the second bit... Could you explain?Son
Link to comment
Share on other sites

Look at the first line of your function:

function datadump ($table) {	$result .= "# Dump of $table \n";

That is the same as saying $result = $result . "# Dump of $table \n";. The problem is that $result is not defined, you're trying to append something to the end of an undefined value.

Link to comment
Share on other sites

  • 3 months later...
Look at the first line of your function:
function datadump ($table) {	$result .= "# Dump of $table \n";

That is the same as saying $result = $result . "# Dump of $table \n";. The problem is that $result is not defined, you're trying to append something to the end of an undefined value.

Thanks for reply, will look into this again...Son
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...