โปรแกรม import ฐานข้อมูล .sql
การใช้งานให้สร้าง database เปล่าๆลงใน MySQL ก่อน แล้วค่อยใช้โปรแกรมนี้ทำการ Import ครับ โดยการกำหนดชื่อ database ที่สร้างให้กับโปรแกรม แล้วทำการ Browse ไฟล์ .sql ได้เลยครับ
โปรแกรมนี้สามารถทำงานกับคำสั่ง CREATE และ INSERT เท่านั้นนะครับ
<?php
$charset = ( isset( $_POST[charset] ) ) ? $_POST[charset] : 2;
$fileupload = $_FILES[fileupload];
$host = ( isset( $_POST[host] ) ) ? $_POST[host] : "localhost";
$username = ( isset( $_POST[username] ) ) ? $_POST[username] : "root";
$password = ( isset( $_POST[password] ) ) ? $_POST[password] : "1234";
$dbname = ( isset( $_POST[dbname] ) ) ? $_POST[dbname] : "sd_blog";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="th-TH">
<head>
<title>MySQL database importer</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo ($charset == 2)?'UTF-8':'TIS-620';?>" />
<style type="text/css">
* {
margin:0;
padding:0;
font-family:Tahoma,"MS Sans Serif";
font-size:100%;
}
body {
font-size:10pt;
}
table {
margin:0 auto;
float:none;
margin-top:50px;
}
form {
border:1px solid #cc6633;
background-color:#fffbf0;
width:500px;
display:table;
}
p {
margin-bottom:5px;
}
p.head {
text-align:center;
font-weight:bold;
background-color:#ffcc99;
border-bottom:1px solid #cc6633;
padding:3px;
}
p.submit {
background-color:#f1f1f1;
padding:3px 0;
margin:0;
border-top:1px solid #cc6633;
}
input,select {
margin-left:5px;
}
option {
padding:1px 2px;
}
label {
width:110px;
text-align:right;
float:left;
}
input#submit {
margin-left:115px;
}
.error {
color: red;
}
.success {
color: green;
}
strong {
color:#000000;
}
</style>
</head>
<body>
<?php
if ( $fileupload[tmp_name] != '' )
{
//เชื่อมต่อกับ database
$dbconnection = @mysql_connect( $host , $username , $password );
if ( $dbconnection )
{
if ( $charset == 1 )
{
mysql_query( "SET NAMES 'tis620';" );
};
$db = mysql_select_db( $dbname );
};
if ( !$dbconnection || !$db ) //ไม่สามารถเชื่อมต่อได้
{
echo '<p><center><strong>Error! ::</strong> <span class="error">'.mysql_error()."</span></center></p>
";
}
else
{
$f = fopen( $fileupload[tmp_name] , 'rb' );
$data = fread( $f , filesize( $fileupload[tmp_name] ) );
fclose( $f );
preg_match_all( '/CREATE(.*);/Usi' , $data , $creates );
for ( $i = 0 ; $i < count( $creates[0] ) ; $i++ )
{
if ( @mysql_query( $creates[0][$i] ) )
{
echo '<p class="success"><strong>Execute ::</strong> '.preg_replace( array( "/ /Usi" , "/
/Usi" , "/
/Usi" ) , array( " " , "" , "<br />" ) , $creates[0][$i] )."</p>";
}
else
{
echo '<div>'.preg_replace( array( "/ /Usi" , "/
/Usi" , "/
/Usi" ) , array( " " , "" , "<br />" ) , $creates[0][$i] ).' <p class="error"><strong>Error ::</strong> '.mysql_error()."</p></div>";
};
};
echo "<br />";
preg_match_all( '/INSERT(.*);/Usi' , $data , $inserts );
for ( $i = 0 ; $i < count( $inserts[0] ) ; $i++ )
{
if ( @mysql_query( $inserts[0][$i] ) )
{
echo '<p class="success"><strong>Execute ::</strong> '.$inserts[0][$i]."</p>";
}
else
{
echo '<div>'.$inserts[0][$i].' <p class="error"><strong>Error ::</strong> '.mysql_error()."</p></div>";
};
};
};
};
?>
<table align="center" cellpadding="0" cellspacing="0">
<tr><th>Import .sql into MySQL database</th></tr>
<tr><td> </td></tr>
<tr><td style="text-align:center">please! create database before import file</td></tr>
<tr><td> </td></tr>
<tr><td style="text-align:center">program by :: <a href="https://www.goragod.com">Goragod.com</a></td></tr>
<tr><td> </td></tr>
<tr><td> </td></tr>
<tr><td>
<form action="import.php" method="post" enctype="multipart/form-data">
<p class="head">Import File</p>
<p><label for="charset">Charset :</label>
<select name="charset" id="charset">
<option value="0"<?php echo ( $charset == 0 ) ? ' selected="selected"' : ''?>>TIS-620</option>
<option value="1"<?php echo ( $charset == 1 ) ? ' selected="selected"' : ''?>>TIS-620(PHP5)</option>
<option value="2"<?php echo ( $charset == 2 ) ? ' selected="selected"' : ''?>>UTF-8</option>
</select>
</p>
<p><label for="host">Host name :</label><input type="text" name="host" id="host" size="45" value="<?php echo $host?>" /></p>
<p><label for="username">User name :</label><input type="text" name="username" id="username" size="45" value="<?php echo $username?>" /></p>
<p><label for="password">Password :</label><input type="text" name="password" id="password" size="45" value="<?php echo $password?>" /></p>
<p><label for="dbname">Database name :</label><input type="text" name="dbname" id="dbname" size="45" value="<?php echo $dbname?>" /></p>
<p><label for="fileupload">Browser :</label><input type="file" name="fileupload" id="fileupload" size="45" /></p>
<p class="submit"><input type="submit" name="submit" id="submit" class="button" value="Go." /></p>
</form>
</td></tr></table>
</body>
</html>