View Single Post
Old 03-07-2009, 01:44 PM   #3 (permalink)
Sakakuchi
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Could look something like this?!
!!! THIS CODE IS NOT TESTED !!!
(just written right out of my head - it could be filled with bugs )
PHP Code:
include("db_connection.php");

function 
CreateBackup($db,$table)
{
  
//select database where table is saved
  
$backup "USE $db;\n";
  
$result mysql_query(SHOW CREATE TABLE '$table') or die(mysql_error());
  while(
$row mysql_fetch_array($result))
  {
    for (
$i=1;$i<count($row)-1;$i++)
      {
         
$backup .= $row[$i];
      }
  }
    
$backup .= "\n;";
  
$result mysql_query("SELECT * FROM '$table'") or die (mysql_error());
 WHILE (
$row mysql_fetch_array($result))
  {
    
$backup .= "INSERT INTO $table VALUES(";
    for (
$i=0;$i<count($row);$i++)
      {
        
$cont $row[$i];
        
$backup .= "'$cont',";
      }
      
//we need 2 cut the last "," away
      
$backup substr($backup,0,strlen($backup)-1);
  }
  
$backup .= ");";
  return 
$backup;

Now you just need to write the backup to an file like:
PHP Code:
$content CreateBackup("mydatabase""mytable");
file_put_contents($filename$content); 
Hope this was helpfull

Edit: When anyone finds mistakes/errors - please drop a reply - I'm always willing to learn...
Sakakuchi is offline  
Reply With Quote
The Following 2 Users Say Thank You to Sakakuchi For This Useful Post:
digitak (03-07-2009), iflashlord (05-03-2009)