Jump to content

MySQL backup problems


sford999

Recommended Posts

Hi,

 

I`m using the following code to list the tables, adding a checkbox next to the tables so I can select what tables I want to back up.

 

However after submitting the form, I`m having this error.

 

Warning: Invalid argument supplied for foreach() in C:\Server\htdocs\admin\inc\functions.php on line 6228

 

That line is:

foreach ($tables as $table)

in the performExport($tables) function.

 

I`ve stared at this for hours now and I can`t see a problem.

 

 

Code in functions.php:

<?php

function getTables()
{
dbconnect();
$result = mysql_query("SHOW TABLES") or die("can't perform query. ".mysql_error());
while ($row = mysql_fetch_array($result))
{
	$tables[] = $row[0];
}
return $tables;
}

function renderPickerForm()
{
global $lang;

$class1 = "bbox";
$class2 = "tables";
$row_count = 0;

ad_header($ad_title = 'Backup Database');

$tables = getTables();
$string = '<form method="post" action="database.html?backup">';
$string .= '<table width="100%" border="0">
  <tr>
    <td colspan="2" class="copyright"><div align="center">'.$lang[db_backup_title].'</div></td>
  </tr>';
foreach ($tables as $table)
{
	$row_class = ($row_count % 2) ? $class1 : $class2;
	$string .= '<tr>
    <td class="'.$row_class.'">'.$table.'</td>
    <td class="'.$row_class.'"><input type="checkbox" value="'.$table.'" name="table[]" /></td>
  </tr>';
	$row_count++;
}
$string .= '<tr>
    <td class="tdup2"><div align="right">'.$lang[global_select_all].'</div></td>
    <td class="tdup2"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></td>
  </tr>
  <tr>
    <td colspan="2" class="copyright"><div align="center">';
$string .= '<input type="submit" name="submit" value="export" class="button" />';
$string .= '</div></td>
  </tr>
</table>';
$string .= '</form>';
return $string;

ad_footer();
}

function returnTablesToExport()
{
if(!isset($_POST['table']))
{
	return false;
}
if(!is_array($_POST['table']))
{
	return false;
}

$_table = array();

$permittedtables = getTables();

foreach ($_POST['table'] as $table)
{
	if(in_array($table, $permittedtables))
	{
		$_table[] = $table;
	}
}
return $_table;
}

if($fh = fopen($filename, "x"))
{
	echo 'Cannot open file ('.$filename.') for writing';
	exit();
}
foreach ($tables as $table)
{
	$sql = "SELECT * FROM $table";
	$result = mysql_query($sql);

	while ($row = mysql_fetch_assoc($result))
	{
		$insert = "";
		foreach ($row as $key=>$val)
		{
			$insert .= "`$key` = '".mysql_escape_string($val)."',";
		}
		if(fwrite($fh, "INSERT INTO $table SET ". rtrim($insert,",") . ";\r\n") === FALSE)
		{
			echo 'Cannot write to file'. $filename;
			exit();
		}
	}
	if(fwrite($fh, "\r\n\r\n####################################\r\n\r\n") === FALSE)
	{
		echo 'Cannot write to file'. $filename;
		exit();
	}
}
fclose($fh);
}

?>

 

 

Code in database.html

<?php

if($tables = returnTablesToExport() === false)
{
echo renderPickerForm();
}
else
{
performExport($tables);
}

Link to comment
https://forums.phpfreaks.com/topic/75104-mysql-backup-problems/
Share on other sites

I missed the part, but here's the full function

 

<?php

function performExport($tables)
{
set_time_limit(0);
$filename = BACKUP_DIR.'backup_'.time().'.sql';

$fh = fopen($filename, "x");

foreach ($tables as $table)
{
	$sql = "SELECT * FROM $table";
	$result = mysql_query($sql);

	while ($row = mysql_fetch_assoc($result))
	{
		$insert = "";
		foreach ($row as $key=>$val)
		{
			$insert .= "`$key` = '".mysql_escape_string($val)."',";
		}
		if(fwrite($fh, "INSERT INTO $table SET ". rtrim($insert,",") . ";\r\n") === FALSE)
		{
			echo 'Cannot write to file'. $filename;
			exit();
		}
	}
	if(fwrite($fh, "\r\n\r\n####################################\r\n\r\n") === FALSE)
	{
		echo 'Cannot write to file'. $filename;
		exit();
	}
}
fclose($fh);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/75104-mysql-backup-problems/#findComment-379853
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.