user906379 user906379 - 1 month ago 7x
MySQL Question

Dump SQL as plain text using PHP

Basically what I need to do is dump a SQL database in plain text using PHP. Just like phpMyAdmin does when you choose not to get a file while exporting.

This way I can send it via mail and reinsert it later using a SQL query.

Is this even possible?



Hope it helps


$dbhost   = "HOSTNAME";
$dbuser   = "USERNAME";
$dbpwd    = "PASSWORD";
$dbname   = "DATABASENAME";

$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";
passthru("/usr/bin/mysqldump --opt --host=$dbhost --user=$dbuser
--password=$dbpwd $dbname > $dumpfile");

// report - disable with // if not needed
// must look like "-- Dump completed on ..." 

echo "$dumpfile "; passthru("tail -1 $dumpfile");