title: MySQL data backup using PHP
body: <p>I am here stuck with an problem and really need you people's help. I am trying to export mysql data using a PHP script. But the problem is an error which is below
  "Fatal error: Allowed memory size of 67108864 bytes exhausted....". I have seen some posts where they have suggested to change the php.ini file. But as I am hosted on a dedicated server I do not have that access. The table that I am trying to export has more the 2.2GB of data. I am here by posting the function that I am using to export those datas. Can you guys please help me in solving this issues?</p>
  
  <pre><code>&lt;?php
  set_time_limit(0);
  backup_tables('hostname','username','password','databasename');
  
  function backup_tables($host,$user,$pass,$name,$table = 'Table Name')
  {
  
  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  
  $result = mysql_query('SELECT * FROM '.$table);
  $num_fields = mysql_num_fields($result);
  
  $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().'-'.($tables).'.sql','w+');
  fwrite($handle,$return);
  fclose($handle);
  }
  ?&gt;
  </code></pre>