Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Connect to database
- $db = mysql_select_db($this->DATABASE_DB);
- if (!$db) {
- die ('Can't use foo : ' . mysql_error());
- }
- if (!empty($db)) {
- // Get all table names from database
- $c = 0;
- $result = mysql_list_tables($this->DATABASE_DB);
- for($x = 0; $x < mysql_num_rows($result); $x++) {
- $table = mysql_tablename($result, $x);
- if (!empty($table)) {
- $arr_tables[$c] = mysql_tablename($result, $x);
- $c++;
- }
- }
- // Structure Header
- $structure .= "-- n";
- $structure .= "-- Backup effettuato il `".date('Y-m-d H:i:s')."` n";
- $structure .= "-- Backup per database `{$this->DATABASE_DB}` n";
- $structure .= "-- nn";
- $structure .= "-- nn";
- $structure .= "-- nn";
- $structure .= "SET NAMES utf8; n";
- $structure .= "SET FOREIGN_KEY_CHECKS = 0;";
- $structure .= "-- nn";
- $structure .= "-- nn";
- // List tables
- $dump = '';
- for ($y = 0; $y < count($arr_tables); $y++){
- // DB Table name
- $table = $arr_tables[$y];
- // Structure Header
- $structure .= "-- n";
- $structure .= "-- Table structure for table `{$table}` n";
- $structure .= "-- nn";
- // Dump Structure
- $structure .= "DROP TABLE IF EXISTS `{$table}`; n";
- $structure .= "CREATE TABLE `{$table}` (n";
- $result = mysql_db_query($this->DATABASE_DB, "SHOW FIELDS FROM `{$table}`");
- while($row = mysql_fetch_object($result)) {
- $structure .= " `{$row->Field}` {$row->Type}";
- $structure .= (!empty($row->Default)) ? " DEFAULT `{$row->Default}`" : false;
- $structure .= ($row->Null != "YES") ? " NOT NULL" : false;
- $structure .= (!empty($row->Extra)) ? strtoupper(" {$row->Extra}") : false;
- $structure .= ",n";
- }
- $structure = ereg_replace(",n$", "", $structure);
- // Save all Column Indexes in array
- unset($index);
- $result = mysql_db_query($this->DATABASE_DB, "SHOW KEYS FROM `{$table}`");
- while($row = mysql_fetch_object($result)) {
- if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) {
- $index['PRIMARY'][$row->Key_name] = $row->Column_name;
- }
- if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) {
- $index['UNIQUE'][$row->Key_name] = $row->Column_name;
- }
- if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) {
- $index['INDEX'][$row->Key_name] = $row->Column_name;
- }
- if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) {
- $index['FULLTEXT'][$row->Key_name] = $row->Column_name;
- }
- }
- // Return all Column Indexes of array
- if (is_array($index)) {
- foreach ($index as $xy => $columns) {
- $structure .= ",n";
- $c = 0;
- foreach ($columns as $column_key => $column_name) {
- $c++;
- $structure .= ($xy == "PRIMARY") ? " PRIMARY KEY (`{$column_name}`)" : false;
- $structure .= ($xy == "UNIQUE") ? " UNIQUE KEY `{$column_key}` (`{$column_name}`)" : false;
- $structure .= ($xy == "INDEX") ? " KEY `{$column_key}` (`{$column_name}`)" : false;
- $structure .= ($xy == "FULLTEXT") ? " FULLTEXT `{$column_key}` (`{$column_name}`)" : false;
- $structure .= ($c < (count($index[$xy]))) ? ",n" : false;
- }
- }
- }
- $structure .= "n);nn";
- // Header
- $structure .= "-- n";
- $structure .= "-- Dumping data for table `$table` n";
- $structure .= "-- nn";
- // Dump data
- unset($data);
- //mysql_set_charset('latin1');
- $result = mysql_query("SELECT * FROM `$table`");
- $num_rows = mysql_num_rows($result);
- $num_fields = mysql_num_fields($result);
- for ($i = 0; $i < $num_rows; $i++) {
- $row = mysql_fetch_object($result);
- $data .= "INSERT INTO `$table` (";
- // Field names
- for ($x = 0; $x < $num_fields; $x++) {
- $field_name = mysql_field_name($result, $x);
- $data .= "`{$field_name}`";
- $data .= ($x < ($num_fields - 1)) ? ", " : false;
- }
- $data .= ") VALUES (";
- // Values
- for ($x = 0; $x < $num_fields; $x++) {
- $field_name = mysql_field_name($result, $x);
- $data .= "'" . str_replace('"', '"', mysql_escape_string($row->$field_name)) . "'";
- $data .= ($x < ($num_fields - 1)) ? ", " : false;
- }
- $data.= ");n";
- }
- $data.= "n";
- $dump .= $structure . $data;
- $dump .= "-- --------------------------------------------------------nn";
- }
- return $dump;
- }
- }
- SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';
Add Comment
Please, Sign In to add comment