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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.