<?php

	include "library.php";

	$cxn = start_code( true );
	
	$pathroot = val( $_SESSION, "pathroot" );


			/* start page */

	page_header( "", $cxn, "", true );

	$today = date("Y_m_d");


			/* loop through each database table */

	$query1 = "SHOW TABLES";

	$result1 = run_query( $query1, $cxn );

	for ($k=0; $k < mysql_num_rows($result1); $k++)
	{
		$row = mysql_fetch_row($result1);

		$table = $row[0];

		if ($table != "page_loads")
		{
			$fp = fopen( $pathroot."/csv_backups/x_".$today."_".$table.".csv", "w" );


					/* process one table */

//			$query = "SELECT * FROM ".$table." ORDER BY id";
			$query = "SELECT * FROM ".$table;

			$result = run_query( $query, $cxn );

			for ($j=0; $j < mysql_num_fields($result); $j++)
					fprintf( $fp, "%s", "\"".mysql_field_name($result,$j)."\"," );

			fprintf( $fp, "\n" );

			for ($i=0;$i < mysql_num_rows($result);$i++)
			{
				$row = mysql_fetch_row($result);

				for($j=0; $j < mysql_num_fields($result); $j++)
				{
					$text = str_replace( "\"", "\\\"", $row[$j] );

					fprintf( $fp, "%s", "\"".$text."\"," );
				}
			
				fprintf( $fp, "\n" );
			}

			fclose( $fp );
		}
	}


	echo black( "Finished" );
?>

