<?php
backup_tables('localhost','root','','easynet_ac');
$return='';

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
	
	$link = mysqli_connect($host,$user,$pass);
	mysqli_select_db($name,$link);
	
	mysqli_query($connection,"set character_set_client='utf8'"); 
	mysqli_query($connection,"set character_set_results='utf8'"); 
	
	mysqli_query($connection,"set collation_connection='utf8_general_ci'"); 
	
	//get all of the tables
	if($tables == '*')
	{
		$tables = array();
		$result = mysqli_query($connection,'SHOW TABLES');
		while($row = mysqli_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	$return='';
	//cycle through
	foreach($tables as $table)
	{
		$result = mysqli_query($connection,'SELECT * FROM '.$table);
		$num_fields = mysqli_num_fields($result);
		
		// $return.= 'DROP TABLE '.$table.';';
		$row2 = mysqli_fetch_row(mysqli_query($connection,'SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysqli_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j<$num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = preg_replace('/\n/','\\n',$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}
		$return.="\n\n\n";
	}
	
	//save file
	$handle = fopen('E://backup\db-backup-Accounting-'.date('Y-m-d-H-i-s').'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);
}

?>

<script>
window.close();
</script>