title: PHP MySQL Dump Script Help
body: <p>I'm trying to create a script to create a dump of a mysql database, however, I can't get it working. Below is the code I'm suing.</p>
  
  <pre><code>    $file = 'backup_'.substr(md5(TIME_NOW), 0, 10).random_str(30);
      header('Content-Type: text/x-sql');
      header('Content-Disposition: attachment; filename="'.$file.'.sql"');            
  
      // process backup
      $time = date('dS F Y \a\t H:i', TIME_NOW);
      $header = "-- Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
      $contents = $header;
  
      $table_selects = array();
      $table_list = $db-&gt;list_tables($config['database']['database']);
      foreach($table_list as $id =&gt; $table_name)
      {
          $table_selects[$table_name] = $table_name;
      }
  
      $field_list = array();
      $fields_array = $db-&gt;show_fields_from($table_name);
      foreach($fields_array as $field)
      {
          $field_list[] = $field['Field'];
      }       
  
      $query = $db-&gt;simple_select($table);
      while($row = $db-&gt;fetch_array($query))
      {
          $insert = "INSERT INTO {$table} ($fields) VALUES (";
          $comma = '';
          foreach($field_list as $field)
          {
              if(!isset($row[$field]) || is_null($row[$field]))
              {
                  $insert .= $comma."NULL";
              }
              else
              {
                  $insert .= $comma."'".$db-&gt;escape_string($row[$field])."'";
              }
              $comma = ',';
          }
          $insert .= ");\n";
          $contents .= $insert;
          clear_overflow($fp, $contents);
      }
  </code></pre>
  
  <p>But it's not working...and I know I have some of the code wrong, such as what tables to select (I want to select all the tables).</p>