Hi, Im trying to write a php backup script that can backup all or user defined tables within my mysql database, currently the script works fine when tables is set to all (*), but when I try and pass user defined tables to the function it throws this error; Warning: mysql_num_fields() expects parameter 1 to be resource, boolean given Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given which refers to the foreach loop The $_POST data comes from a single text box form Heres the script; $table = '*'; if(isset($_POST['tables'])){ print_r($_POST); mysqlBackup($_POST['tables']); } //MySQL function mysqlBackup($tables) { $script = ''; //Get tables if($tables == '*') { $tables = array(); $query = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($query)) { $tables[] = $row[0]; } } else { $tables = explode(',', $tables); } print_r($tables); //Cycle results foreach($tables as $table) { $query = mysql_query('SELECT * FROM '.$table); $fields = mysql_num_fields($query); //Drop tables $script .= 'DROP TABLE ' . $table . ';'; //Create tables $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $script .= "\n\n" . $row2[1] . ";\n\n"; //Insert data for($i = 0; $i < $fields; $i++) { while($row = mysql_fetch_row($query)) { //Loop rows $script .= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j < $fields; $j++) { //Loop Fields $row[$j] = addslashes($row[$j]); $row[$j] = preg_replace('/\n/', '\\n', $row[$j]); if(isset($row[$j])) { $script .= '"' . $row[$j] . '"'; } else { $script .= '""'; } if($j<($fields-1)) { $script .= ','; } } $script .= ");\n"; } } $script .= "\n\n\n"; } //Backup filename with date and extension $date = date('H_i__D_d_m_Y'); $file = '../scripts/backups/dbbackup_'.$date.'.sql'; //Save file $handle = fopen($file, 'w+'); fwrite($handle, $script); fclose($handle); echo("Backup successfull! Click <a href='$file'>here</a> to download"); } PHP: Tried printing out the $_POST and $tables array within the script to see if that was the issue, results below; form data Array ( [tables] => users,stock [Backup_Tables] => Submit ) after explode Array ( [0] => users [1] => stock ) As far as I can see the arrays look correct, and the table names are present in my database. Can anyone see the problem? Thanks in advance
check your query first.. echo 'SELECT * FROM '.$table; // add this line $query = mysql_query('SELECT * FROM '.$table); PHP: when you see the query generated, try to execute it on you phpmyadmin or mysql query browser the error was caused by a query error, maybe the table does not exist.. or do this.. ... $query = mysql_query('SELECT * FROM '.$table); if (mysql_errno()) { exit(mysql_error()); } ... PHP: it will display the error, if it was encountered while inside the loop..
Tried the mysql_errno() and it seems that the problem was it not selecting the database. When I called the function from my tools file to backup all the tables, it was using the include on that page. But when I pass the individual table names to it, it changes to the script page which didn't have an include. Managed to get it working by just including my database connect file at the top of the function. Thanks for your help