<!-- taken a form with a submit button -->
<form name="form1" method="post" action="">
<input type="submit" name="restore" id="restore" value="Restore">
</form>
<?php
/* Check if the button is pressed */
if(isset($_POST['restore'])) {
// Name of the file. Give path here
$filename = 'p2p-pro.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = 'pass';
// Database name. Optional. Not needed as we are creating new database
//$mysql_database = date('Y-m-d');
// Connect to MySQL server
$con = mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
$db_selected = mysql_select_db($mysql_database);/* or die('Error selecting MySQL database: ' . mysql_error());*/
if(!$db_selected) {
/* getting current year and next year */
$year = date('Y');
$year = $year.'-'.($year+1);
/* assigning database name as p2p_pro with current and next year concatenated */
$date = 'p2p_pro'.$year;
/* creating database with assigned name */
$sql = "CREATE DATABASE `$date`";
mysql_select_db($date);
mysql_query($sql) or die (mysql_error());
/* using the newly created database to work with */
$sql = "USE `$date`";
mysql_select_db($date);
mysql_query($sql) or die (mysql_error());
//$sql = 'CREATE DATABASE $mysql_database';
/*if (mysql_query($sql, $con)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}*/
}
// Temporary variable used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if(substr(trim($line), -1, 1) == ';') {
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Database imported successfully";
}
?>
Tally XML and PHP Integration Samples
ReplyDeleteTally XML Request + PHP sport