Forse potrebbe tornare utile lo script per intero.
Codice PHP:
<?
//script to install databases for php/mysql book
//to_install is the name of the database
//filename are in the form dbname.dump
mysql_connect("localhost", "nobody", "") or die
("<h3>Before Installing your Databases you must have MySQL installed and provide a valid host,
username, and password in line two of the install.dbs file.</h3>");
$all_dbs = array ("guestbook2k", "catalog", "discussion","netsloth", "survey", "tracking");
if(isset($submit))
{
$to_install = array();
if(isset($passed_dbs) && is_array($passed_dbs) && isset($install_all))
{
echo "Let's try this again. This time, please select the
databases you would like installed OR check Install All.
Not both. Press the back button, please.";
exit;
} elseif (isset($passed_dbs) && is_array($passed_dbs))
{
$to_install = $passed_dbs;
} elseif (isset($install_all))
{
$to_install = $all_dbs;
}
else
{
echo "Let's try this again. This time, please select the
databases you would like installed OR check Install All.
Press the back button, please.";
exit;
}
// $path = $DOCUMENT_ROOT . "/book/install/";
foreach($to_install as $dbname)
{
//create and slect database
mysql_query("drop database if exists $dbname") or die
(mysql_error() . "Could not drop database $dbname ");
mysql_query("create database $dbname") or die
(mysql_error() . "Could not create database $dbname ");
mysql_select_db($dbname);
//files will come in the form dbname.dump
//$file_to_parse = $path . $dbname . ".dump";
$file_to_parse = $dbname . ".dump";
if (!file_exists($file_to_parse))
{
echo "Could not find '$file_to_parse'. Are your files placed properly? Please check";
exit;
}
if (!$fp = fopen($file_to_parse, "r"))
{
echo "Read of $file_to_parse failed. Please check permissions.";
exit;
}
$contents = fread($fp, filesize ($file_to_parse));
fclose($fp);
//parse out drop, create, and insert statments
preg_match_all("/(DROP TABLE.*?);/s", $contents, $drop_statements);
preg_match_all("/(CRE.*?);/s", $contents, $create_statements);
preg_match_all("/(INSER.*?);[\n\r]/s", $contents, $insert_statements);
foreach($drop_statements[1] as $query)
{
mysql_query($query) or die
(mysql_error(). " drop table failed: $query.");
}
foreach($create_statements[1] as $query)
{
mysql_query($query) or die
(mysql_error(). " create table failed: $query.");
}
foreach($insert_statements[1] as $query)
{
mysql_query($query) or die
(mysql_error() . "insert failed: $query.");
}
echo "database \"$dbname\" successfully created.
\n";
}
}
else
{
?>
<h2>Welcome to the Applications for MySQL/PHP Applications Book</h2>
If you are seeing this, you have PHP and MySQL installed and talking to each other. Cool</p>
To get the applications working, you first need to install the databases with the sample data. Below
please choose the databases you would like installed. Choose the individual databases or mark Install
ALL.</p>
[b]NOTE: If you have databases by the same name on your MySQL server they will be
deleted! Beware.[/b]</p>
Databases to install:</p>
<form action = <? echo $REQUEST_URI; ?> method="get">
<?
foreach($all_dbs as $dbname){
echo "<input type=\"checkbox\" name=\"passed_dbs[]\" value=\"$dbname\">$dbname
\n";
}
?>
<input type="checkbox" name="install_all" value="yes">Install All
<input type="submit" name="submit" value="Submit">
</form>
<? } ?>