spacepoet Posted August 5, 2011 Share Posted August 5, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/243966-error-doing-database-back-up/ Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 I've removed your database connection credentials, but you should consider changing your password. Quote Link to comment https://forums.phpfreaks.com/topic/243966-error-doing-database-back-up/#findComment-1252719 Share on other sites More sharing options...
spacepoet Posted August 5, 2011 Author Share Posted August 5, 2011 Hi: Thanks for that, but I changed the credentials to "fake" ones before posting I never post that stuff online Any idea what is causing the issue? Maybe because I have tables in the DB that are not in use ?? Quote Link to comment https://forums.phpfreaks.com/topic/243966-error-doing-database-back-up/#findComment-1252728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.