Location: PHPKode > scripts > PHP MySQL Smart > php-mysql-smart/dump.php
<?
session_start();
set_time_limit(500);

/**********************************************
* 2007 by Alexander Ottitzky
* http://www.powerweb99.at/
* for free use, no warranty at all
* please do not remove copyright
**********************************************/
function get_table_structure($db,$tb){
	global $con;
	mysql_select_db($db,$con) or die("select error!");
    $a = "\ndrop table if exists $tb;\ncreate table $tb (";
	$sql = "describe $tb";
	$result = mysql_query($sql) or die("<b>SQL Error</b><br>".mysql_error());
	while($rs = mysql_fetch_array($result)) {
        $null = " NOT NULL";
		if($rs[2]) $null = " NULL";
		$default = "";
		if($rs[4]) $default = " default '$rs[4]'";
		if($rs[3]=="PRI") $primary = "\nPRIMARY KEY  (`$rs[0]`),";
		if($rs[3]=="MUL") $key.= "\nKEY `$rs[0]` (`$rs[0]`),";
        $a.="\n`$rs[0]` $rs[1] $null$default $rs[5],";
    }
	$a.="$primary$key";
	if($a) $a = substr($a,0,strlen($a)-1);
	$a.="\n);\n\n";
	mysql_free_result($result);
	return $a;
}

function backup($table){
	global $con,$backub_database;				
	echo(get_table_structure($backub_database, $table));
	$sql= ("select * from $table");
	$result = mysql_query($sql) or die("SQL ERROR");
	while($rs = mysql_fetch_object($result)) {		         
		$b = ("insert into $table values (");
		foreach($rs as $tmp1 => $key){
			$b.= "'".addslashes($key)."',";
		}
		echo(substr($b,0,strlen($b)-1).");\n");
	}
	mysql_free_result($result);	
	echo("\n");				
}

$backub_database = addslashes($_REQUEST[db]);
$backub_table = addslashes($_REQUEST[tb]);

if($_SESSION[LOGIN] && ($backub_database || $backub_table)){
	if(!$user) $user = $_SESSION[LOGIN][USER];
	if(!$pass) $pass = $_SESSION[LOGIN][PASS];
	if(!$host) $host = $_SESSION[LOGIN][HOST];
	$con = @mysql_connect($host, $user, $pass) or die("Connection Error");
	if ($con){
		header( 'Content-type: application/octet-stream' );
		if($backub_table){
			header( "Content-Disposition: attachment; filename=dump_$backub_table.sql");
			backup($backub_table);
			
		}else{
			header( "Content-Disposition: attachment; filename=dump_$backub_database.sql");
			$trs = mysql_list_tables($backub_database);
			$tables=array();
			for($i=0; $i < mysql_num_rows($trs); $i++) {
				backup(mysql_tablename($trs,$i));
			}
		}		
		mysql_close();	
	}	
}
  
?>
Return current item: PHP MySQL Smart