title: Export mysql table into SQL format?
body: <p>Is it possible to export a table with its records into SQL format via PHP code ? I've been looking around and all I found is exporting it as CSV file.</p>
  
  <p>However I found the code below :</p>
  
  <pre><code>backup_tables('localhost','root','','compare_db');
  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.= "\n\n".$row2[1].";\n\n";
  
          for ($i = 0; $i &lt; $num_fields; $i++) 
          {
              while($row = mysql_fetch_row($result))
              {
                  $return.= 'INSERT INTO '.$table.' VALUES(';
                  for($j=0; $j&lt;$num_fields; $j++) 
                  {
                      $row[$j] = addslashes($row[$j]);
                      $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                      if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                      if ($j&lt;($num_fields-1)) { $return.= ','; }
                  }
                  $return.= ");\n";
              }
          }
          $return.="\n\n\n";
      }
  
      //save file
      $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
      fwrite($handle,$return);
      fclose($handle);
  }
  </code></pre>
  
  <p>It outputs the following error :</p>
  
  <blockquote>
    <p>Notice: Undefined variable: return </p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>Deprecated: Function ereg_replace() is deprecated </p>
  </blockquote>
  
  <p>All I want is to export a SINGLE table via PHP into .sql format !....I'm not that familiar with PHP, but hope you can help me out !</p>