I'm trying to create a page that allows people to backup their database on a web page but I'm having trouble with the ending of it.
<?php
require("../include/config.php");
$tables = array();
$qTables = mysql_query("SHOW TABLES");
while($row = mysql_fetch_row($qTables))
{
$tables[] = $row[0];
}
foreach($tables as $tab1)
{
$return.= "DROP TABLE IF EXISTS `".$tab1."`;";
$row2 = mysql_fetch_row(mysql_query("SHOW CREATE TABLE `" . $tab1 . "`"));
$return.= "\n\n".$row2[1].";\n\n";
$result = mysql_query("SELECT * FROM ".$tab1) or die(mysql_error());
$num_fields = mysql_num_fields($result);
$return.= "INSERT INTO `".$tab1."`";
$col = mysql_query('SELECT * FROM '.$tab1);
$a = 0;
$return.= " (";
while ($a < mysql_num_fields($col)) {
$meta = mysql_fetch_field($col, $a);
$return.= "`" . "$meta->name";
$a++;
if ($a < mysql_num_fields($col)) {
$return.= "`,";
} else {
$return.= "`";
}
}
$return.= ")";
$return.=" VALUES\n(";
for ($i = 0; $i < $num_fields; $i++){
while($row = mysql_fetch_row($result)){
for($j=0; $j<$num_fields; $j++){
if (isset($row[$j])) {
$return.= "'".$row[$j]."'" ;
} else {
$return.= "''";
}
if ($j < ($num_fields-1)) {
$return.= ",";
}
if (j < ($num_fields-1)){
$return.= "),\n(";
} else {
$return.= ");\n";
}
}
}
}
}
$handle = fopen("db-backup-".time()."-".(md5(implode(",",$tables))).".sql","w+");
fwrite($handle,$return);
?>
At this part:
if (j < ($num_fields-1)){
$return.= "),\n(";
} else {
$return.= ");\n";
}
If it has finished going through all of the values it will put ); at the end and if it hasn't it will put ), and then continue with the next one. The problem I'm having is it's only doing the ),
Can someone help me please?