title: how to create a backup database script in php and zip it
body: <p>what is the best way to backup a database using php and backing data from mysql,i have tried to look for tutorials online but i seem not to understand nicely well i wrote this though its not working nicely,im getting these errors and warnings</p>
  
  <pre><code>Warning: gzopen(backups/adminpanel/adminpanel_aggrement_1367616442.sql.gz): failed to open stream: No such file or directory in C:\xampp\htdocs\how are things\admin panel\backup.php on line 17
  
  Notice: Undefined variable: time in C:\xampp\htdocs\how are things\admin panel\backup.php on line 30
  The file--backups/adminpanel/aggrement_.sql.gz--could not be opened for writing.
  </code></pre>
  
  <p>the code on line 17 and 30 is this</p>
  
  <pre><code>if ($fp = gzopen ("$dir/{$db_name}_{$table}_{$bu_time}.sql.gz", 'w9')) {
  
  
  
    echo "&lt;p&gt;The file--$dir/{$table}_{$time}.sql.gz--could not be opened for writing.&lt;/p&gt;\n";
  </code></pre>
  
  <p>this is my code</p>
  
  <pre><code>&lt;?php 
  $db_name = 'adminpanel';
  $dir = "backups/$db_name";
  if (!file_exists('path/to/directory')) {
      @mkdir('path/to/directory');
  }
  $bu_time = time();
  $dbc = @mysqli_connect (localhost, root, nokiae71, adminpanel);
  $q = 'SHOW TABLES';
  $r = mysqli_query($dbc, $q);
  if (mysqli_num_rows($r) &gt; 0) {
      echo "&lt;p&gt;Backing up database '$db_name'.&lt;/p&gt;\n";
      while (list($table) = mysqli_fetch_array($r, MYSQLI_NUM)) {
          $q2 = "SELECT * FROM $table";
          $r2 = mysqli_query($dbc, $q2);
          if (mysqli_num_rows($r2) &gt; 0) {
              if ($fp = gzopen ("$dir/{$db_name}_{$table}_{$bu_time}.sql.gz", 'w9')) {
                                  while ($row = mysqli_fetch_array($r2, MYSQLI_NUM)) {
                      foreach ($row as $value) { 
  
                          gzwrite ($fp, "'$value', ");
                      }
                      gzwrite ($fp, "\n"); 
  
                  }               
                  // Close the file:
                  gzclose ($fp); 
  
              } else { // Could not create the file!
                  echo "&lt;p&gt;The file--$dir/{$table}_{$time}.sql.gz--could not be opened for writing.&lt;/p&gt;\n";
                  break; 
              }   
          }
      } 
  } else {
      echo "&lt;p&gt;The submitted database--$db_name--contains no tables.&lt;/p&gt;\n";
  }
  
  ?&gt;
  </code></pre>
  
  <p>how can i make this work or is there any better script i can use out there...</p>