Jump to content

[SOLVED] Listing mysql tables in a select menu


pcw

Recommended Posts

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 :)

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.