Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class dump{
- var $no_error = 0;
- var $query_id = 0;
- var $query_count = 0;
- var $query_time = 0;
- var $query_array = array();
- var $table_fields = array();
- function get_table_def_mysql($table, $crlf = "<br>") {
- global $site_db, $tables_info;
- $dump .= "\r\n## Ломаем таблицу " . $table . "\r\n";
- $dump .= "DROP TABLE IF EXISTS " . $table . ";\r\n";
- $dump .= "## Сструктура таблицы " . $table . "\r\n";
- $this->query('SET SQL_QUOTE_SHOW_CREATE = 0');
- if($row = $this->query_firstrow("SHOW CREATE TABLE " . $table)){
- $dump .= str_replace("\n", $crlf, $row[1]);
- }
- $dump .= " ;";
- $this->free_result();
- return $dump;
- }
- function query_firstrow($query = "") {
- if($query != ""){
- $this->query($query);
- }
- $result = $this->fetch_array($this->query_id);
- $this->free_result();
- return $result;
- }
- function query($query = ""){
- if($query != ""){
- if((defined("PRINT_QUERIES") && PRINT_QUERIES == 1) || (defined("PRINT_STATS") && PRINT_STATS == 1)){
- $startsqltime = explode(" ", microtime());
- }
- if(!$this->query_id = @mysql_query($query)){
- $this->error("<b>Bad SQL Query</b>: ".htmlentities($query)."<br /><b>".mysql_error()."</b>");
- }
- if((defined("PRINT_QUERIES") && PRINT_QUERIES == 1) || (defined("PRINT_STATS") && PRINT_STATS == 1)){
- $endsqltime = explode(" ", microtime());
- $totalsqltime = round($endsqltime[0]-$startsqltime[0]+$endsqltime[1]-$startsqltime[1],3);
- $this->query_time += $totalsqltime;
- $this->query_count++;
- }
- if(defined("PRINT_QUERIES") && PRINT_QUERIES == 1){
- $query_stats = htmlentities($query);
- $query_stats .= "<br><b>Querytime:</b> ".$totalsqltime;
- $this->query_array[] = $query_stats;
- }
- return $this->query_id;
- }
- }
- function fetch_array($query_id = -1, $assoc = 0){
- if($query_id != -1){
- $this->query_id = $query_id;
- }
- if($this->query_id){
- return ($assoc) ? mysql_fetch_assoc($this->query_id) : mysql_fetch_array($this->query_id);
- }
- }
- function free_result($query_id = -1){
- if($query_id != -1){
- $this->query_id = $query_id;
- }
- return @mysql_free_result($this->query_id);
- }
- function get_numrows($query_id = -1){
- if($query_id != -1){
- $this->query_id = $query_id;
- }
- return mysql_num_rows($this->query_id);
- }
- function get_insert_id(){
- return ($this->connection) ? @mysql_insert_id($this->connection) : 0;
- }
- function get_numfields($query_id = -1){
- if($query_id != -1){
- $this->query_id = $query_id;
- }
- return @mysql_num_fields($this->query_id);
- }
- function get_fieldname($query_id = -1, $offset){
- if($query_id != -1){
- $this->query_id = $query_id;
- }
- return @mysql_field_name($this->query_id, $offset);
- }
- function get_fieldtype($query_id = -1, $offset){
- if($query_id != -1){
- $this->query_id = $query_id;
- }
- return @mysql_field_type($this->query_id, $offset);
- }
- function get_table_fields($table){
- if(!empty($this->table_fields[$table])){
- return $this->table_fields[$table];
- }
- $this->table_fields[$table] = array();
- $result = $this->query("SHOW FIELDS FROM $table");
- while($row = $this->fetch_array($result)){
- $this->table_fields[$table][$row['Field']] = $row['Type'];
- }
- return $this->table_fields[$table];
- }
- function error($errmsg, $halt = 0){
- if(!$this->no_error){
- global $user_info;
- if(isset($user_info['user_level']) && $user_info['user_level'] == ADMIN){
- echo "<br /><font color='#FF0000'><b>DB Error</b></font>: ".$errmsg."<br />";
- } else {
- echo "<br /><font color='#FF0000'><b>An unexpected error occured. Please try again later." . $errmsg . "</b></font><br />";
- }
- if($halt){
- exit;
- }
- }
- }
- function get_table_content_mysql($table, $crlf = "<br>") {
- $dump .= "\r\n";
- $dump .= "##\r\n##Дамп данных для таблицы " . $table . "\r\n##\r\n";
- $result = $this->query("SELECT * FROM " . $table);
- if($result && mysql_num_rows($result)){
- $column_list = "";
- $num_fields = @mysql_num_fields($result);
- for($i = 0; $i < $num_fields; $i++){
- $column_list .= (($column_list != "") ? ", " : "").$this->get_fieldname($result, $i);
- }
- }
- while($row = mysql_fetch_array($result)){
- $dump .= "\r\nINSERT INTO " . $table . " (" . $column_list . ") VALUES (";
- for($i = 0; $i < $num_fields; $i++){
- $dump .= ($i > 0) ? ", " : "";
- if(!isset($row[$i])){
- $dump .= "NULL";
- }
- elseif($row[$i] == "0" || $row[$i] != ""){
- $type = @mysql_num_fields($result, $i);
- if($type == "tinyint" || $type == "smallint" || $type == "mediumint" || $type == "int" || $type == "bigint"){
- $dump .= $row[$i];
- }
- else {
- $search_array = array('\\', '\'', "\x00", "\x0a", "\x0d", "\x1a");
- $replace_array = array('\\\\', '\\\'', '\0', '\n', '\r', '\Z');
- $row[$i] = str_replace($search_array, $replace_array, $row[$i]);
- $dump .= htmlspecialchars("'".$row[$i]."'");
- }
- }
- else {
- $dump .= "''";
- }
- }
- $dump .= ');';
- }
- return $dump;
- }
- function dumping(){
- $tables = array(
- 'storm_admin','storm_cats','storm_news','storm_conf','storm_comm'
- );
- $d .= "## Storm Dumper for Storm News CMS\r\n";
- $d .= "## Version 0.1B\r\n";
- $d .= "## Start\r\n\r\n";
- foreach ($tables as $table) {
- @set_time_limit(1200);
- //echo "##\r\n## Структура таблицы ".$table."\r\n##\r\n";
- $d .= $this->get_table_def_mysql($table, "\r\n");
- $d .= $this->get_table_content_mysql($table, "\r\n");
- }
- $d .= "\r\n##END";
- return $d;
- }
- }
Add Comment
Please, Sign In to add comment