Jump to content

trying to make a function to backup Mysql dbase - issues...


scotch33

Recommended Posts

Hi there,

I am trying to provide a customer with a backup button to my sqldump a database.  the code (kindly provided in [url=http://www.devpapers.com/article/55]http://www.devpapers.com/article/55[/url] is...
[code]
$db_host = "localhost";
$db_name = "mydbasename";
$db_user = "mydbaseusername";
$db_pass = "mydbasepassword";

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";
  }

$dvd_orders = datadump ("dvd_orders");
$products = datadump ("products");

$content = $dvd_orders . $products;

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

However I am now getting the following results...

Warning: Cannot modify header information - headers already sent by (output started at /home/pathto/backup.php:11) in /home/pathto/backup.php on line 42 Warning: Cannot modify header information - headers already sent by (output started at /home/pathto/backup.php:11) in /home/pathto/backup.php on line 43 # Dump of dvd_orders # Dump DATE : 19-Dec-2006 INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); INSERT INTO dvd_orders VALUES("","","","","","","","","","","","","","","","","","","",""); # Dump of products # Dump DATE : 19-Dec-2006 INSERT INTO products VALUES("","","","");

At the moment there is no other code on the php page other than the function - do i need to put some kind of header in to tell the page that it is just executing this script?

Thanks!

John
Link to comment
Share on other sites

a couple of things...

1) did you read this?? http://www.phpfreaks.com/forums/index.php/topic,37442.0.html  -- it's one of the stickies in this forum and it goes all sorts of into headers.  I don't fully understand the massive problems with this item but there is a lot of info; mostly related to passing HTML tags prior to the header commands...

2) Your values in your SQL statements appear to be blank.  If that's the case, do some error checking to see why your variables are not acquiring their desired values;  this in turn, may lead you to the end problem.

3) make sure the username you're using to get into the database has the appropriate rights to perform all of the fucntions your script is wanting it to do.
Link to comment
Share on other sites

Hi there,

Thanks!  that was a useful article - however, I have done a little with headers before and the first thing I did check was if there was anything there.  There is nothing at all in the page before the <?php start, not even white space.

Your second point - I am not sure where to even start with error checking on this so iof anyone can give me a pointer or point me at articales related I would appreciate it.

the database has only one usename and password, which are the ones I am using to get to the database.  they are also used on all the other pages in the site accessing the database and there is no problem with them.

Any further help would be appreciated.

Thanks again.
Link to comment
Share on other sites

Thanks Orio.
This looks ideal.  One question - whilkst reading the instructions it mentions
[quote]If running from shell, put this above the <?php  "#! /usr/bin/php -q"  without the quotes!!![/quote]

It further mentions that if I am going to cron this and keep the database safe,. then this is teh best option.  Am i right in assuming the running from shell, is running from the root folder rather than the public_html folder?

Thanks!

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