1

What's wrong with this code?I'm currently calling the code below using a link. Is there anything else needed for this to work? The error is specifically in this line:

$backupdir = 'C:\wampbackup\x.sql'; 

What might be a possible solution to this. I'm a beginner so please bear with my lack of knowledge.

  $host= 'localhost';          
  $user= 'root';                
  $pass= ''; 
  $db=   'hospital; 

  $backupdir = 'C:\wampbackup\x.sql';    

  // Compute day, month, year, hour and min. 
  $today = getdate(); 
  $day = $today[mday]; 
  if ($day < 10) { 
      $day = "0$day"; 
  } 
  $month = $today[mon]; 
  if ($month < 10) { 
      $month = "0$month"; 
  } 
  $year = $today[year]; 
  $hour = $today[hours]; 
  $min = $today[minutes]; 
  $sec = "00"; 


  // Execute mysqldump command. 
  // It will produce a file named $db-$year$month$day-$hour$min.gz 
  // under $DOCUMENT_ROOT/$backupdir 
  system(sprintf( 
    'mysqldump --opt -h %s -u %s -p%s %s | gzip > %s/%s/%s-%s%s%s-%s%s.gz',                                                  
    $host, 
    $user, 
    $pass, 
    $db, 
    getenv('DOCUMENT_ROOT'), 
    $backupdir, 
    $db, 
    $year, 
    $month, 
    $day, 
    $hour, 
    $min 
  ));  
  echo '+DONE';  
?> 

3 Answers 3

1

Change this line:

$backupdir = 'C:\wampbackup\x.sql'; 

into:

$backupdir = 'C:\\wampbackup\\x.sql'; 

You need to escape back-slashes with another \ back-slash. :)

Sign up to request clarification or add additional context in comments.

2 Comments

still not working after following your advice. Where would the backup suppose to go?
it will be saved in C:\wampbackup\x.sql. Make sure that you have write permission for the folder C:\wampbackup
1

You should actually post your error message along with your code. But if it is not a copy-paste error, I found your bug: It's on line 4, the closing apostrophe is missing

$db=   'hospital;

Comments

0

As a minor aside, you could skip the entirety of

if ($day < 10) { 
    $day = "0$day"; 
}

Later on you're using those values in a sprintf() call, which can do such formatting automatically. Simply change the matching %s with %02d (print a decimal padded to 2 spaces with the 0 character).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.