I've got a function to make a backup of the database:
Warning: mysql_connect(): Access denied for user ''@'localhost' (using password: NO) in C:\Web\administracion\11.php on line 251 -> this is the line: mysql_connect();
the file does correctly generates and saves all data. but I this error.
the code is placed correctly in the file: blankpage.php
how could remove the error on the file it generates, it is only the first line in which it appears, the rest of the file if all data from the backup of the database appears
This is the code:
<?php
backup_tables($conn);
function backup_tables($conn,$tables = '*')
{
mysql_connect();
//$conn = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');
// mysql_select_db($name,$link);
$return = '' ;
//get all of the tables
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
//if (empty($result)) continue;
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
// If you are getting errors related to fetching, uncomment the next row:
//if (empty($result)) continue;
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
// If you are getting errors related to fetching, uncomment the next row:
//if (empty($result)) continue;
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
// If you are getting errors related to fetching, uncomment the next row:
//if (empty($result)) continue;
$columns = count($row);
for($j=0; $j<$columns; $j++)
{
$row[$j] = addslashes($row[$j]);
//$row[$j] = ereg_replace("\n","\\n",$row[$j]);
$row[$j] = preg_replace("/\r\n/","\\r\\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
$filename = 'CopiaSeg-'. date("d-M-Y-H-i-s") .'.sql';
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$filename");
echo $return;
}
?>