note 23819 added to function.mysql-query
| From: | monte at ispi dot net | Date: | Mon, 29 Jul 2002 16:28:08 +0000 |
| Subject: | note 23819 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33875@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);
?>
--
http://www.php.net/manual/en/function.mysql-query.php
http://master.php.net/manage/user-notes.php?action=edit+23819
http://master.php.net/manage/user-notes.php?action=delete+23819
http://master.php.net/manage/user-notes.php?action=reject+23819