Jump to content

Recommended Posts

Hi:

 

I have a script I use to allow the user to make a backup of the database. They click a lick and it goes to the below code, and writes a back-up of the database onto a folder on the server.

 

This is the code:

<?php
include('../include/myConn.php');

backup_tables('DB credentials removed');

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
   
   $link = mysql_connect($host,$user,$pass);
   mysql_select_db($name,$link);
   
   //get all of the tables
   if($tables == '*')
   {
      $tables = array();
      $result = mysql_query('SHOW TABLES');
      while($row = mysql_fetch_row($result))
      {
         $tables[] = $row[0];
      }
   }
   else
   {
      $tables = is_array($tables) ? $tables : explode(',',$tables);
   }
   
   //cycle through
   foreach($tables as $table)
   {
      $result = mysql_query('SELECT * FROM '.$table);
      $num_fields = mysql_num_fields($result);
      
      $return.= 'DROP TABLE '.$table.';';
      $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
      $return.= "\n\n".$row2[1].";\n\n";
      
      for ($i = 0; $i < $num_fields; $i++) 
      {
         while($row = mysql_fetch_row($result))
         {
            $return.= '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])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
               if ($j<($num_fields-1)) { $return.= ','; }
            }
            $return.= ");\n";
         }
      }
      $return.="\n\n\n";
   }
   
   
      
   //save file
   //$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
   
   $handle = fopen('NLCDBBackUp-'.date("m.d.y").'-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
   
   fwrite($handle,$return);
   fclose($handle);

}

header ("Location: a_Home.php");

?>

 

I get this error:


Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource in /home/content/64/8119464/html/admin/a_Export.php on line 32

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/content/64/8119464/html/admin/a_Export.php on line 35

Warning: Cannot modify header information - headers already sent by (output started at /home/content/64/8119464/html/admin/a_Export.php:32) in /home/content/64/8119464/html/admin/a_Export.php on line 68

 

Line 32:

$num_fields = mysql_num_fields($result);

 

 

Line 35:

$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));

 

 

I'm confuse because on some sites it works fine, while on others it gives me the error.

Even with the error it still writes the copy of the database properly, but I would rather not have the error appear.

 

Anyone know why this is happening? Is there a way to suppress the error?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/243966-error-doing-database-back-up/
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.