php備份數(shù)據(jù)庫類的方法
PHP是一種 HTML 內(nèi)嵌式的語言,是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語言,語言的風(fēng)格有類似于C語言,被廣泛地運(yùn)用。大家知道php備份數(shù)據(jù)庫類嗎?下面我們就給大家詳細(xì)介紹一下吧!我們積累了一些經(jīng)驗,在此拿出來與大家分享下,請大家互相指正。
php備份數(shù)據(jù)庫類分享1
/**
*
* @name php備份數(shù)據(jù)庫
* @param string $DbHost 連接主機(jī)
* @param string $DbUser 用戶名
* @param string $DbPwd 連接密碼
* @param string $DbName 要備份的數(shù)據(jù)庫
* @param string $saveFileName 要保存的文件名, 默認(rèn)文件保存在當(dāng)前文件夾中,以日期作區(qū)分
* @return Null
* @example backupMySqlData('localhost', 'root', '123456', 'YourDbName');
*
*/
function backupMySqlData($DbHost, $DbUser, $DbPwd, $DbName, $saveFileName = '')
{
header("Content-type:text/html;charset=utf-8");
error_reporting(0);
set_time_limit(0);
echo '數(shù)據(jù)備份中,請稍候......
';
$link = mysql_connect($DbHost, $DbUser, $DbPwd) or die('數(shù)據(jù)庫連接失敗: ' . mysql_error());
mysql_select_db($DbName) or die('數(shù)據(jù)庫連接失敗: ' . mysql_error());
mysql_query('set names utf8');
/pic/p>
$isDropInfo = '';
$SQL = '';
$row = array();
$tables = array();
$tableStructure = array();
$fileName = ($saveFileName ? $saveFileName : 'MySQL_data_bakeup_') . date('YmdHis') . '.sql';
/pic/p>
$res = mysql_query("SHOW TABLES FROM $DbName");
while ($row = mysql_fetch_row($res)) {
$tables[] = $row[0];
}
mysql_free_result($res);
/pic/p>
foreach ($tables as $val) {
$res = mysql_query("show create table $val", $link);
$row = mysql_fetch_row($res);
$isDropInfo = "DROP TABLE IF EXISTS `" . $val . "`;rn";
$tableStructure = $isDropInfo . $row[1] . ";rn";
file_put_contents($fileName, $tableStructure, FILE_APPEND);
mysql_free_result($res);
}
/pic/p>
foreach ($tables as $val) {
$res = mysql_query("select * from $val");
/pic/p>
while ($row = mysql_fetch_row($res)) {
$sqlStr = "INSERT INTO `".$val."` VALUES (";
foreach($row as $v){
$sqlStr .= "'$v',";
}
/pic/p>
$sqlStr = substr($sqlStr, 0, strlen($sqlStr) - 1);
$sqlStr .= ");rn";
file_put_contents($fileName, $sqlStr, FILE_APPEND);
}
mysql_free_result($res);
}
echo '數(shù)據(jù)備份成功!';
}
/pic/p>
backupMySqlData('localhost', 'root', '123456', 'YouDbName');
?>
【php備份數(shù)據(jù)庫類的方法】相關(guān)文章:
php數(shù)據(jù)庫備份腳本10-10
PHP框架:CodeIgniter框架備份數(shù)據(jù)庫03-17
PHP數(shù)據(jù)庫連接的方法08-11
PHP插入數(shù)據(jù)庫的方法03-12
PHP封裝數(shù)據(jù)庫操作類12-05