#!/usr/local/bin/php
<?php

// http://www.shortlearner.com/export-mysql-database-using-php/

	// Database configuration
	//$host = "localhost";
	//$username = "root";
	//$password = "";
	//$database_name = "find";
	
	//echo "Debug...";
	
	$db_host    = "analogdepat.mysql.db";
   	$db_user   = "analogdepat";
 	$db_pass   = "Un92pac007";
	$db_name   = "analogdepat";

	// Get connection object and set the charset
	//$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
	//$conn->set_charset("utf8");

	// Get All Table Names From the Database
	//$tables = array();
	//$sql = "SHOW TABLES";
	//$result = mysqli_query($conn, $sql);
	//while ($row = mysqli_fetch_row($result)) 
	//{
	//	$tables[] = $row[0];
	//}

	$sqlScript = "";
	
	$tables= "BLITZ_affectation,BLITZ_classement,BLITZ_evolution,BLITZ_parties";

	$tables = is_array($tables) ? $tables : explode(',',$tables);

	//print_r($tables);

/*
	foreach ($tables as $table) 
	{
        // Prepare SQLscript for creating table structure
		$query = "SHOW CREATE TABLE $table";
		$result = mysqli_query($conn, $query);
		$row = mysqli_fetch_row($result);
		$sqlScript .= "\n\n" . $row[1] . ";\n\n";
    
		$query = "SELECT * FROM $table";
		$result = mysqli_query($conn, $query);
		$columnCount = mysqli_num_fields($result);
		
		// Prepare SQLscript for dumping data for each table
		for ($i = 0; $i < $columnCount; $i ++) 
		{
			while ($row = mysqli_fetch_row($result)) 
			{
				$sqlScript .= "INSERT INTO $table VALUES(";
				
				for ($j = 0; $j < $columnCount; $j ++) 
				{
					$row[$j] = $row[$j];
					if (isset($row[$j])) 
					{
						$sqlScript .= '"' . $row[$j] . '"';
					} 
					else 
					{
						$sqlScript .= '""';
					}
					
					if ($j < ($columnCount - 1)) 
					{
						$sqlScript .= ',';
					}
				}
				
				$sqlScript .= ");\n";
			}
		}
		
		$sqlScript .= "\n"; 
	
	
	}
*/	
	//echo "<pre>";
	//print_r($sqlScript);
	//echo "<pre/>";

	//$backup_file_name = $db_name . '_backup_' . time() . '.sql';
	
	
	
	$jour = date('d-m-Y');
	$heure = date("H_i_s");
	$infos = $jour.'-'.$heure;
	$backup_file_name = "test1__backup_auto_blitz-" .$infos .".sql";      //////// modif le 30 Juin 2018        aaa
	$chemin = '/homez.65/analogde/www/WORKAREA/WEBSITE/BLITZ/backup_chess';
	
	$myfile = fopen($backup_file_name, 'a+');
	$txt = "Patoche fait des tests\n";
	fwrite($myfile, $txt);
	$txt = "Pourquoi tant de haine...\n";
	fwrite($myfile, $txt);
	$txt = "   Bingo une solution en ligne de mire ?.\n";

	fwrite($myfile, $txt);

	fclose($myfile);
	
	/*
		$fp = fopen($fichier, 'a+'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);
 */
	//if(!empty($sqlScript))
	//{
		// Save the SQL script to a backup file

		
/*		
			$backup_file_name = $database_name . '_backup_' . time() . '.sql';
			$fileHandler = fopen($backup_file_name, 'w+');
			$number_of_lines = fwrite($fileHandler, $sqlScript);
			fclose($fileHandler); 
*/
 
		// Download the SQL backup file to the browser
		/*header('Content-Description: File Transfer');
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename=' . basename($backup_file_name));
		header('Content-Transfer-Encoding: binary');
		header('Expires: 0');
		header('Cache-Control: must-revalidate');
		header('Pragma: public');
		header('Content-Length: ' . filesize($backup_file_name));
		ob_clean();
		flush();
		readfile($backup_file_name);
		exec('rm ' . $backup_file_name); */
	//}

?>