Here's what I was able to create for a PHP "dump" script.
Now revised and tested. This will export the following syntax:
- DROP TABLE IF EXISTS
- CREATE TABLE
- ALTER TABLE ADD FOREIGN KEY
- INSERT INTO TABLE
Here's the script:
<?php
$host = "localhost";
$username = "usr";
$password = "pwd";
$dbname = "my_db";
$table_name = ""; // If set, dumps only the specified table, otherwise dumps all
$file = "dump.sql";
$mysqli = new mysqli($host, $username, $password, $dbname);
// Get a list of tables
$sql = "SELECT TABLE_NAME AS `name` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}'";
if ($table_name) {
$sql .= " AND TABLE_NAME = '{$table_name}'";
}
$db_result = $mysqli->query($sql);
$out = "";
// For each table
while ($table = $db_result->fetch_assoc()) {
// Build the table
$sql = "SHOW CREATE TABLE `{$dbname}`.`{$table['name']}`";
$table_result = $mysqli->query($sql);
if ($create_table = $table_result->fetch_assoc()) {
// Build the DROP TABLE DDL
$out .= "DROP TABLE IF EXISTS `{$dbname}`.`{$table['name']}`;
";
// Build the CREATE TABLE DDL
$out .= $create_table['Create Table'] . ";
";
// Build the FOREIGN KEY DDL for the table
$sql = "SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '{$table['name']}' AND CONSTRAINT_SCHEMA = '{$dbname}' AND CONSTRAINT_NAME != 'PRIMARY'";
$fk_result = $mysqli->query($sql);
while ($fk = $fk_result->fetch_assoc()) {
if ($fk['REFERENCED_COLUMN_NAME']) {
$out .= "ALTER TABLE `{$dbname}`.`{$fk['TABLE_NAME']}` ADD CONSTRAINT `{$fk['CONSTRAINT_NAME']}` FOREIGN KEY (`{$fk['COLUMN_NAME']}`) REFERENCES `{$fk['REFERENCED_TABLE_NAME']}` (`{$fk['REFERENCED_COLUMN_NAME']}`);
";
}
}
$fk_result->close();
$out .= "
";
// Build the INSERT DML for the table
$sql = "SELECT * FROM `{$table['name']}`";
$data_result = $mysqli->query($sql);
while ($row = $data_result->fetch_assoc()) {
$keys = array_keys($row);
array_walk($keys, function(&$key) {
$key = "`{$key}`";
});
$keys = implode(", ", $keys);
$values = array_values($row);
array_walk($values, function(&$val) {
$val = "'{$val}'";
});
$values = implode(", ", $values);
$out .= "INSERT INTO `{$dbname}`.`{$table['name']}` ({$keys}) VALUES ({$values});
";
}
$data_result->close();
$out .= "
";
}
$table_result->close();
}
$db_result->close();
file_put_contents($file, $out);
Here's example output for a single-table dump in my DB I tested on:
DROP TABLE IF EXISTS `user_group`;
CREATE TABLE `user_group` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
ALTER TABLE `user_group` ADD CONSTRAINT `user_group_ibfk_1` FOREIGN KEY (`type_id`) REFERENCES `user_group_type` (`id`);
INSERT INTO `user_group` (`id`, `type_id`, `name`, `created`) VALUES ('1', '1', 'Administrator', '2011-10-01 22:58:29');
INSERT INTO `user_group` (`id`, `type_id`, `name`, `created`) VALUES ('2', '1', 'Moderator', '2011-10-01 22:58:29');
INSERT INTO `user_group` (`id`, `type_id`, `name`, `created`) VALUES ('3', '1', 'Registered User', '2011-10-01 22:58:29');