原本想著用SHOW CREATE TABLE ergodic 來做備份的但是發現如果Table 有 comment 而且還是亂碼的話,會導致SHOW CREATE TABLE 出來的腳本缺少一個符號。所以有了這個版本。
-
class MysqlExport{
-
- /**
- * 資料庫連線
- */
- private $_db;
- /**
- * 建立表格結構sql
- */
- private $_create_table = '';
-
- public function __construct($host = '', $user = '', $ pass = '', $db = '', $port = 3306) {
- if (empty($host) ||empty($user)) {
-
- } else {
- $this - >real_connect($host, $user, $pass, $db, $port);
- }
- }
-
- public function init() {
- return $this;
- }
-
- /**
- * 連接資料庫
- */
- public function real_connect($host, $user, $pass, $db = '', $port = 3306) {
- $this- >_db = mysql_connect($host . ':' . $port, $user, $pass);
- if ($db) {
- $this->select_db($db);
- }
- return $this->init();
- }
-
- /**
- * 選擇資料庫
- */
- public function select_db($db) {
- if (mysql_select_db($ db, $this-this-this-this- >_db)) {
- return true;
- }
- }
-
- /**
- * 查詢語句
- */
- public function query($sql) {
- if ($ this->_db) {
- if ($this->_resource = mysql_query($sql, $this->_db)) {
- return $this->init( );
- }
- }
- 拋出新例外($this->get_error());
- }
-
- /**
- * 取得結果集
- */
- 公用函數fetch_array($arg = MYSQL_BOTH) {
- $result = array();
- if ($this->_resource && !mysql_errno($this->_db)) {
- while ($rs = mysql_fetch_array($this->_resource, $arg)) {
- $result[] = $rs;
- }
- }
- return $result;
- }
-
- /**
- * 取得錯誤
- */
- 公用函數get_error() {
- return mysql_errno($this->_db) 。 "n";
- }
-
- /**
- * 顯示資料表
- */
- public function show_tables($table = '') {
- $sql = "SHOW TABLES";
- $sql .= $表 ? " LIKE '{$table}'" : '';
- $result = $this->query($sql)->fetch_array(MYSQL_ASSOC);
- return $result;
- }
-
- /**
- * 顯示資料表欄位
- */
- public function show_columns($table) {
- if (empty($table)) {
- return array();
- }
- $sql = "顯示{$table} 中的完整欄位";
- $result = $this->query($sql)->fetch_array(MYSQL_ASSOC);
- return $result;
- }
-
- /**
- * 顯示資料表狀態
- */
- public function show_table_status($table) {
- if (empty($table)) {
- return array();
- }
- $result🎜 > $result🎜 > $result🎜 > $result🎜 > $result🎜 > $result🎜 > $result🎜 > $result🎜 > $result🎜 > $result = $this->query("SHOW TABLE STATUS LIKE '{$table}'")->fetch_array(MYSQL_ASSOC);
- $result = reset($result);
- return $result ;
- } }
-
- /**
- * 顯示資料表結構
- */
- public function show_create_table($table) {
- if (empty($table)) {
- return '';
- }
- }
- $this->_create_table = "如果不存在`{$table}`(" . PHP_EOL;
- $table_status = $this->show_table_status($table);
- $columns = $this->show_columns( $table)
-
- foreach ($columns AS $col) {
- $this->_create_table .= "`{$col['Field']; }` {$col['Type']} NOT NULL {$col['Extra']}," . PHP_EOL;
- }
-
- $this->_create_table .= $this->create_indexSyntax($table);
- $ char = substr($table_status['Collate'], 0, strpos($table_status['Collation'], '_'));
- $table_status['Auto_increment'] = $table_status[ '自動增量'] ? $table_status['Auto_increment'] : 0;
- $this->_create_table .= ")Engine={$table_status['Engine']} AUTO_INCRMENT={$table_status['Auto_increment']} 預設字元集={$字元};” str_repeat(PHP_EOL, 3);
-
- return $this->_create_table;
- }
-
- /**
- * 顯示觸發器
- */
- public function show_constraint($ 🎜> if (empty($db_name)) {
- return array();
- }
- $sql = "SELECT a.CONSTRAINT_NAME AS constrint_name, a.TABLE_name AS table_name, a.COLU .REFERENCED_TABLE_NAME 作為引用的表名稱、a.REFERENCED_COLUMN_作為引用的列名稱、b.UPDATE_RULE 作為更新規則、b.DELETE_RULE AS 刪除規則FROM information_schema.KEY_COLUMN_USAGE 為項目基金constraint_name E a.constraint_schema = ' {$db_name}' AND a.POSITION_IN_UNIQUE_CONSTRAINT = 1";
- $result = $this->query($sql)->fetch_array(MYSQL_ASSOC);
- }
- 🎜> /**
- * 顯示索引
- */
- public function show_index($table) {
- if (empty($table)) {
- return array();
- }
- $sql = "SHOW INDEX FROM {$table}";
- $result = $this->query($sql)->fetch_array(MYSQL_ASSOC);
- return $result;
- }
-
- /**
- * 顯示資料庫結構
- */
- public function show_database_char() {
- $sql = "顯示變數如'character_set_database'";
- $char = $this->; query($sql)->fetch_array(MYSQL_ASSOC);
- return reset($char);
- }
-
- /**
- * 建立索引語法
- */
- public function create_indexSyntax($table) {
- if (empty($table)) {
- return array();
- }
- $indexing = $this->show_index($table);
- $syntax = array () ;
- $indexSyntax = array();
- foreach ($indexing as $index) {
- $syntax[$index['Index_type']][$index['Key_name']][] = $ index['Column_name'];
- }
-
- foreach ($syntax as $index_type => $index_value) {
- foreach ($index_value as $key_name => $columns) {
- foreach ($index_value as $key_name => $columns) {
- if ($key_name == 'PRIMARY') {
- $indexSyntax[] = 'PRIMARY KEY (`' . implode("`,`", $columns) . '`)';
- } else {
- if ($index_type == 'FULLTEXT') {
- $indexSyntax[] = "FULLTEXT KEY `{$key_name}` (`" .內爆("`,`", $columns) 。 '`)' ;
- } else{
- $indexSyntax[] = "KEY `{$key_name}` 使用{$index_type} (`" . implode("`,`", $columns) . '` )';
- }
- }
- }
- }
-
- return implode(',' . PHP_EOL, $indexSyntax) 。 🎝>*/
- public function create_insertSyntax($table) {
- if (empty($table)) {
- return '';
- }
- $sql = "SELECT * FROM {$table}";
- $result = $this->query($sql)->fetch_array(MYSQL_ASSOC);
- $insertStr = '' ;
- if ($result) {
- $first = reset($result);
- $key = implode('`,`', array_keys($first));
- $insert = " INSERT INTO `{$table}` (`{$key}`) VALUES ";
- $valuesStr = array();
- foreach ($result as $value) {
- $values = array () ;
- foreach ($value as $v) {
- $v = mysql_real_escape_string($v);
- $values[] = preg_replace("#\ #", "\", $v) ;
- }
- $valuesStr[] = "('" .implode("','", $values) . "')";
- }
- $valuesStr = array_chunk($valuesStr, 5000) ;
- foreach ($valuesStr as $str) {
- $insertStr .= $insert .內爆(',', $str) 。 PHP_EOL;
- }
-
- }
- 回傳 $insertStr 。
- $test = new MysqlExport('localhost', 'root', '', 'pm_cms');
- $char = $test->show_database_char();
- $test->query("設定名稱{$char['Value']}");
- $tables = $test->show_tables();
- foreach ($tables as $table) {
- list($table_name) = array_values ( $table);
- $export .= $test->show_create_table ($table_name);
- $export .= $test->create_insertSyntax($table_name);
- }
- $fp = fopenopen ( 'pm_cms.sql', 'w');
- fwrite ($fp, $export);
- fclose($fp);
- ?>
- 複製程式碼
-
-
|