Hi…
I’ve seen code for back up database but when I run the code the database was backup outside the folder. I want to put the back up database inside the folder
here is the code:
<?php
include 'config.php';
backup_tables('localhost','root','','payroll');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\
\
".$row2[1].";\
\
";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\
","\\\
",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\
";
}
}
$return.="\
\
\
";
}
//save file
$myfoldername = "backup_DBPayroll";//your folders name
$handle = fopen(getcwd().$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
// $handle = fopen('db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
$smarty->display('header_cat.tpl');
$smarty->display('backup.tpl');
$smarty->display('footer.tpl');
?>
Thank you in advance
Here’s my guess. These two lines determine the ultimate location of the file:
$myfoldername = "backup_DB_Payroll";
$handle = fopen(getcwd().$myfoldername.'db-backup-'./* etc */);
But if you notice, there’s no slash in between $myfoldername and “db-backup-”. So instead of saving it to folder “backup_DB_Payroll”, it’s just prepending “backup_DB_Payroll” to the filename.
I think.
Actually from my source here is the actual code:
$myfoldername = "backup_DBPayroll";//your folders name
$handle = fopen(getcwd()./.$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
but i got an error so I remove /
thank you
Actually from my source here is the actual code:
$myfoldername = "backup_DBPayroll";//your folders name
$handle = fopen(getcwd()./.$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
but i got an error: Parse error: syntax error, unexpected ‘/’ in C:\xampp\htdocs\payroll\backup_db.php on line 58
so I remove /
thank you
Try moving the slash inside the string, like this:
$handle = fopen(getcwd().$myfoldername.'/db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
I already resolved it now using this code:
<?php
include 'config.php';
backup_tables('localhost','root','','payroll');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\
\
".$row2[1].";\
\
";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\
","\\\
",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\
";
}
}
$return.="\
\
\
";
}
//save file
// $myfoldername = "backup_DBPayroll";//your folders name
// $handle = fopen(getcwd().\\.$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
//$myfoldername = $_SERVER['DOCUMENT_ROOT']."/backup_DBPayroll/";//your folders name
//$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
$myfoldername = "../backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'operatives-db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+');
// $myfoldername = $_SERVER['DOCUMENT_ROOT']."/path-to/backup_DBPayroll/";//your folders name
// $handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
// $handle = fopen('db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
$smarty->display('payroll_report_cat.tpl');
//$smarty->display('header_cat.tpl');
$smarty->display('backup.tpl');
$smarty->display('footer.tpl');
?>
Now, I just want to ask if with this same code file is possible to add code for back up one table as csv file?
If possible how?
Thank you