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"); ?>

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')

  $link = mysql_connect($host,$user,$pass);

  //get all of the tables
  if($tables == '*')
    $tables = array();
    $result = mysql_query('SHOW TABLES');
    while($row = mysql_fetch_row($result))
      $tables[] = $row[0];
    $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";

  //save file
  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

Recommended Answers

All 3 Replies

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.

I can't because every time show me the error :(

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.