0

Hello guys or girls. I have copied from http://davidwalsh.name/backup-mysql-database-php and i have the next error:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/a6704458/public_html/backup_cr.php on line 18

And this is the code:

<?php include("/home/a6704458/public_html/password_protect.php"); ?>
<?php
include("database/database.php");
backup_tables($mysql_host,$mysql_user,$mysql_password,'*');

/* 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+');
  fwrite($handle,$return);
  fclose($handle);
}
?>

Edited by prisacariualex: something wrong

2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by prisacariualex
0

Apparently SHOW TABLES fails, you may not have permission to execute that query, but it could be that the credentials are incorrect. Add some error checking, to find out where the problem is.

Edited by pritaeas

0

Can someone give an tutorial on how to make a backup to a mysql database using php? I readed more about the script from above and sawed is a bad/old script. Please some help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.