note 23819 deleted from function.mysql-query by dams
| From: | dams@php.net | Date: | Tue, 30 Jul 2002 00:36:06 +0000 |
| Subject: | note 23819 deleted from function.mysql-query by dams | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-33930@lists.php.net to get a copy of this message | ||
A script to take the output of mysqldump
and run it through mysql_query. It matches
the queries by beginning with CREATE
or INSERT and ends with ;\n
First, an example of mysqldump:
mysqldump -u user -p DB_NAME > DB.sql
If you only want the db structure (no data) do:
mysqldump --no-data -u user -p DB_NAME > DB.sql
<?php
if(!$link = mysql_connect('hostname','user','pass')) {
echo "could not connect\n";
exit();
}
if(!$fp = fopen('DB.sql','r')) {
echo "unable to open DB.sql, exiting.\n";
exit();
}
$create_tables_sql = fread($fp, filesize('DB.sql'));
fclose($fp);
if(!mysql_select_db('NEW_DB_NAME')) {
echo "unable to select NEW_DB_NAME, exiting.\n";
exit();
}
// match each SQL statement
preg_match_all('!(CREATE|INSERT).*;\n!Us', $create_tables_sql, $match);
foreach($match[0] as $query) {
// execute each statement
if(!mysql_query($query)) {
echo "unable to execute \"$query\", exiting.\n";
exit();
}
}
mysql_close($link);
?>