pcw Posted March 19, 2009 Share Posted March 19, 2009 Hi, I have written this script to simplify the restoration of mysql backups. <?php $cmd = $_GET['cmd']; if ($cmd=="") { $cmd = "choosesql"; } switch($cmd) { case "choosesql": $backupFile = "backup/"; $handle = opendir($backupFile); while ($sqlfile = readdir($handle)) { $sqlfiles[] = $sqlfile; } closedir($handle); sort($sqlfiles); print "<form action='sqlimport.php?cmd=sqlbackup' method='POST'>"; print "<select name='backupFile'>"; print "<option>Choose Database Backup</option>"; foreach ($sqlfiles as $sqlfile) { if (($sqlfile != ".") And ($sqlfile != "..")) { print sprintf("<option value='$backupFile/%s'>%s</option>", $sqlfile, $sqlfile); } } print "</select>"; print "<input type='submit' name='tmpupdate' value='update'>"; print "</form>"; print "</center>"; print ' </td> </tr> </table>'; break; case "sqlbackup": $backupFile = $_POST['backupFile']; $dbhost = 'localhost'; $dbuser = 'moveitho_paul'; $dbpass = 'test'; $db = 'moveitho_sitebuilder'; mysql_connect($dbhost, $dbuser, $dbpass) or die('Error occured whilst connecting to the server: ' . mysql_error()); mysql_select_db($db) or die('Error occured whilst selecting the MySQL database: ' . mysql_error()); $tmpline = ''; $sqlfiles = file($backupFile); foreach ($sqlfiles as $sqlfile) { if (substr($sqlfile, 0, 2) == '--' || $sqlfile == '') continue; $tmpfile .= $sqlfile; if (substr(trim($sqlfile), -1, 1) == ';') { mysql_query($tmpfile) or print('Error occured whilst performing database query \'<strong>' . $tmpfile . '\': ' . mysql_error() . '<br /><br />'); $tmpfile = ''; } } break; } ?> This enables me to select the .sql file from a dropdown menu, click update, and the table is restored. Now I want to reverse the process. I want to be able to select a mysql table from a dropdown menu, submit it, and it creates a backup of that table. I know how to create a .sql backup and have the script to do that. I just dont know how to list the tables in a select menu. Any help is much appreciated Quote Link to comment Share on other sites More sharing options...
pcw Posted March 19, 2009 Author Share Posted March 19, 2009 Ok, I have this now <?php include("data/required.php"); if (!mysql_connect($dbhost, $dbuser, $dbpass)) { echo 'Could not connect to mysql'; exit; } $query = "SHOW TABLES FROM $db"; $result = mysql_query($query); if (!$result) { echo "DB Error, could not list tables\n"; echo 'MySQL Error: ' . mysql_error(); exit; } while ($row = mysql_fetch_row($result)) { $sqlbkups = "{$row[0]} = $sqlbkup;"; } foreach ($sqlbkups as $sqlbkup) { echo "$sqlbkup"; } mysql_free_result($result); ?> Without the foreach part it lists the tables but all in one line. I need to print each one separately as I will be listing them in a dropdown menu. I keep getting this error Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 How do I fix this? Quote Link to comment Share on other sites More sharing options...
pcw Posted March 19, 2009 Author Share Posted March 19, 2009 I have now got <?php include("data/required.php"); if (!mysql_connect($dbhost, $dbuser, $dbpass)) { echo 'Could not connect to mysql'; exit; } $query = "SHOW TABLES FROM $db"; $result = mysql_query($query); if (!$result) { echo "DB Error, could not list tables\n"; echo 'MySQL Error: ' . mysql_error(); exit; } while ($row = mysql_fetch_row($result)) { $val = "{$row[0]}"; foreach ($val as &$value) { $value = $value; } echo "$value"; } mysql_free_result($result); ?> But get this error Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Warning: Invalid argument supplied for foreach() in /home/moveitho/public_html/sitebuilder/sqlexport.php on line 21 Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
pcw Posted March 20, 2009 Author Share Posted March 20, 2009 Has anyone got any idea how to fix this? Thanks Quote Link to comment Share on other sites More sharing options...
irkevin Posted March 20, 2009 Share Posted March 20, 2009 first why are you passing the variable bewteen quotes? foreach ($sqlbkups as $sqlbkup) { echo "$sqlbkup"; // why is this between quotes? huh. } what is on line 21? Quote Link to comment Share on other sites More sharing options...
pcw Posted March 20, 2009 Author Share Posted March 20, 2009 line 21 is foreach ($row as &$value) { i took the " " away and it made no difference Quote Link to comment Share on other sites More sharing options...
pcw Posted March 20, 2009 Author Share Posted March 20, 2009 All I want is for each table to be assigned to a variable so I can display them in a select menu. ie $table1 = 'profiles'; $table2 = 'email'; etc. Is there any way for SHOW TABLES, to assign each of the tables to a variable? Thanks Quote Link to comment Share on other sites More sharing options...
pcw Posted March 20, 2009 Author Share Posted March 20, 2009 Any ideas? Thanks Quote Link to comment 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.