|
|
|
จะทำการ Restore ไฟล์ sql ด้วย php ต้องเขียนยังไงครับ |
|
|
|
|
|
|
|
เขียนยังไงหรอครับ ผมใช้ code นี้ในการ backup
<?php
backup_tables('localhost','root','1234','spas');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen('D:\db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
?>
Code (PHP)
Code (ASP)
Code (PHP)
[php]Code (PHP)
[php]
[/php]
[/php]
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2013-08-13 20:56:15 |
By :
Jaynarak |
View :
832 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูนะครับ ผมเคยทำแต่แบบนี้
Code (PHP)
<!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">
<head>
<link rel="icon" type="image/gif" href="images/images.gif">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$host="";
$user="";
$pw="";
$dbname="";
$day=date("d-m-Y");
$sql = ("mysql --host=$host --user=$user --password=$pw $dbname < Backup_db/db-backup.sql");
exec($sql);
if($sql) {
echo"<script language='JavaScript'>";
echo"alert('ระบบได้ทำการเรียกคืนฐานข้อมูลเรียบร้อยแล้ว');";
echo"window.location='register.php';";
echo"</script>";
}else{
echo"<script language='JavaScript'>";
echo"alert('Error :: ไม่สามารถเรียกคืนฐานข้อมูลได้');";
echo"window.location='register.php';";
echo"</script>";
}
?>
</body>
</html>
|
|
|
|
|
Date :
2013-08-13 22:13:27 |
By :
littlebeer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|