您好,登錄后才能下訂單哦!
使用ThinkPHP怎么備份MySQL數據庫?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
class DBExport { /** * @description 獲取當前數據庫的所有表名。 * @static * @return array */ static protected function getTables() { $dbName=C('DB_NAME'); $result=M()->query("SHOW FULL TABLES FROM `{$dbName}` WHERE Table_Type = 'BASE TABLE'"); foreach ($result as $v){ $tbArray[]=$v['Tables_in_'.C('DB_NAME')]; } return $tbArray; } static protected function getViews() { $dbName=C('DB_NAME'); $result=M()->query("SHOW FULL TABLES FROM `{$dbName}` WHERE Table_Type = 'VIEW'"); foreach ($result as $v){ $tbArray[]=$v['Tables_in_'.C('DB_NAME')]; } return $tbArray; } /** * @description 導出SQL數據,但不包含表創建代碼。 * @static * @return string */ static public function ExportAllData() { $tables = self::getTables(); $arrAll = array( "SET FOREIGN_KEY_CHECKS=0;", self::BuildAllTriggerDropSql(), self::BuildTableSql(), self::BuildViewSql() ); $tbl = new Model(); foreach($tables as $table) { $arrAll[]="\r\nDELETE FROM {$table};"; /* $rs = $tbl->query("SHOW COLUMNS FROM {$table}"); $arrFields = array(); foreach ($rs as $k=>&$v){ $arrFields[] = "`{$v['Field']}`"; } $sqlFields = implode($arrFields,","); */ $rs=$tbl->query("select * from `{$table}`"); foreach ($rs as $k=>&$v){ $arrValues = array(); foreach($v as $key=>$val) { if(is_numeric($val)){ $arrValues[]=$val; }else if(is_null($val)){ $arrValues[]='NULL'; }else{ $arrValues[]="'".addslashes($val)."'"; } } $arrAll[] = "INSERT INTO `{$table}` VALUES (".implode(',',$arrValues).");"; } } $arrAll[]=self::BuildTriggerCreateSql(); return implode("\r\n",$arrAll); } static protected function BuildTableSql() { $tables = self::getTables(); $arrAll = array(); foreach($tables as &$val){ $rs = M()->query("SHOW CREATE TABLE `{$val}`"); $tbSql = preg_replace("#CREATE(.*)\\s+TABLE#","CREATE TABLE",$rs[0]['Create Table']); $arrAll[] = "DROP TABLE IF EXISTS `{$rs[0]['Table']}`;\r\n{$tbSql};\r\n"; } return implode("\r\n",$arrAll); } static protected function BuildViewSql() { $views = self::getViews(); $arrAll = array(); foreach($views as &$val){ $rs = M()->query("SHOW CREATE VIEW `{$val}`"); $tbSql = preg_replace("#CREATE(.*)\\s+VIEW#","CREATE VIEW",$rs[0]['Create View']); $arrAll[] = "DROP VIEW IF EXISTS `{$rs[0]['View']}`;\r\n{$tbSql};\r\n"; } return implode("\r\n",$arrAll); } /** * @description 如果存在觸發器,生成刪除代碼。原因是:插入數據的時候可能會受到觸發器影響。 * @static * @return string */ static public function BuildAllTriggerDropSql() { $rs = M()->query("show triggers"); $arrAll = array(); foreach ($rs as $k=>&$v) { $arrSql = array( 'DROP TRIGGER IF EXISTS `',$v['Trigger'],'`;' ); $arrAll[] = implode('',$arrSql); } return implode("\r\n",$arrAll); } /** * @description 生成所有觸發器的創建代碼。 * @static * @return string */ static protected function BuildTriggerCreateSql() { $rs = M()->query("show triggers"); $arrAll = array(); foreach ($rs as $k=>&$v) { $arrSql = array( 'CREATE TRIGGER `',$v['Trigger'],'` ',$v['Timing'],' ',$v['Event'],' ON `', $v['Table'],'` FOR EACH ROW ',$v['Statement'],';' ); $arrAll[] = implode('',$arrSql); } return implode("\r\n",$arrAll); } }
調用示例:
vendor('DBExport',COMMON_PATH); header('Content-type: text/plain; charset=UTF-8'); $dbName = C('DB_NAME'); header("Content-Disposition: attachment; filename=\"{$dbName}.sql\""); echo DBExport::ExportAllData()
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。