Class Dump (tables mysql)

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Class Dump (tables mysql)

Class Dump (tables mysql)

par bunk » 25 févr. 2016, 14:00

Bonjour,

Je viens d'adapter une fonction de sauvegarde de table (Source)
elle fonctionne je crois assez bien chez moi mais je voulais partager pour l'améliorer.

D'avance merci :
<?php

	class Dump {

		function Dump_Table($table_name){
			$file = 'dump_table_'.$table_name.'_'.date('Y-m-d_H-i-s').'.sql';
			$dir = 'dir/';
			$host = 'localhost';
			$login = 'login';
			$password = 'password';
			$base = 'base';
			try{
				$pdo = new PDO('mysql:host='.$host.';dbname='.$base.';charset=utf8;',
				$login,
				$password,
				array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
				$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);

			}catch(PDOException $e){
				return false
			}
			$array_type = array();
			$entete = "-- ----------------------\n";
			$entete .= "-- ".$base." : ".date("d-M-Y")."\n";
			$entete .= "-- ----------------------\n\n\n";
			$creations = "";
			$insertions = "\n\n";
			$show_tables = 'SHOW TABLES';
			$req_show_tables = $pdo->query($show_tables);
			while($table = $req_show_tables->fetch()){
				if($table[0] != 'logs' && $table[0] == $table_name){
					$creations .= "-- -----------------------------\n";
					$creations .= "-- ".$table[0]."\n";
					$creations .= "-- -----------------------------\n";
					$creations .= "DROP TABLE IF EXISTS `".$table[0]."`;\n";
					$show_create_tables = 'SHOW CREATE TABLE '.$table[0];
					$req_show_create_tables = $pdo->query($show_create_tables);
					while($selected_table = $req_show_create_tables->fetch()){
						$show_colums_tables = 'SHOW COLUMNS FROM '.$table[0];
						$req_show_colums_tables = $pdo->query($show_colums_tables);
						while($colums_tables = $req_show_colums_tables->fetch()){
							foreach($colums_tables as $k => $v){
								if($k == 'field'){
									$array_type[] = array('Field' => $colums_tables['Field'], 'Type' => $colums_tables['Type'], 'Null' => $colums_tables['Null'], 'Default' => $colums_tables['Default']);
								}
							}
						}
						$creations .= $selected_table[1].";\n\n";
					}
					$insertions .= "-- -----------------------------\n";
					$insertions .= "-- ".$table[0]."\n";
					$insertions .= "-- -----------------------------\n";
					$select_data = 'SELECT * FROM '.$table[0];
					$req_select_data = $pdo->query($select_data);
					while($data = $req_select_data->fetch()){
						$insertions .= "INSERT INTO ".$table[0]." VALUES(";
						$pointer = 0;
						$count = 0;
						$total = count($data)/2;
						foreach($data as $k => $v){
							if(!is_numeric($k)){
								$count += 1;
								if($v != ''){
									if(stristr($array_type[$pointer]['Type'], 'varchar') !== FALSE || stristr($array_type[$pointer]['Type'], 'text') !== FALSE || stristr($array_type[$pointer]['Type'], 'blob') !== FALSE){
										$insertions .=  "'".str_replace("'", "''", $v)."'";
									}else{
										if(stristr($array_type[$pointer]['Type'], 'int') === FALSE){
											$insertions .=  "'".str_replace("'", "''", $v)."'";
										}else{
											if(is_numeric($v)){
												$insertions .=  $v;
											}else{
												$insertions .=  "'".str_replace("'", "''", $v)."'";
											}
										}
									}
								}else{
									if($array_type[$pointer]['Null'] == 'YES'){
										if(stristr($array_type[$pointer]['Type'], 'varchar') !== FALSE || stristr($array_type[$pointer]['Type'], 'text') !== FALSE || stristr($array_type[$pointer]['Type'], 'blob') !== FALSE){
											$insertions .= "''";
										}else{
											$insertions .= "NULL";
										}
									}else{
										if(stristr($array_type[$pointer]['Type'], 'enum') !== FALSE){
											$insertions .= "'".$array_type[$pointer]['Default']."'";
										}else{
											$insertions .= "''";
										}
									}
								}
								if($total != $count){
									$insertions .= ", ";
								}
								$pointer += 1;
							}
						}
						$insertions .= ");\n";
					}
				}
			}
			unset($pdo);
			if(!file_exists($dir)) mkdir($dir,0777);
			$content = $entete."\n".$creations."\n".$insertions;
			file_write($dir.DS.$file,$content);
			return true;
		}

	}

?>