Re: Mysql question

From: Date: Wed, 19 Nov 2003 02:25:21 +0000
Subject: Re: Mysql question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-170227@lists.php.net to get a copy of this message
Lists wrote:
I have a db in sql, and I need a php/mysql query/command to copy the entire db (schema and data) to a new db. I know how to do this with a table, but I can not figure out how to do this with a whole db. I also know that I could do it using mysql dump, but I don't want to have to run a shell command.
You'll need to do it with PHP... Here's one method... <?php //connect to database $database = 'xxx'; //name of database to copy $ndatabase = 'yyy'; //name of new database mysql_query("CREATE DATABASE $ndatabase"); $rs = mysql_query("SHOW TABLES FROM $database"); while($r = mysql_fetch_row($rs)) { mysql_select_db($database); $rs2 = mysql_query("SHOW CREATE TABLE {$r[0]}"); $ctable = myqsl_result($rs,0); mysql_select_db($ndatabase); mysql_query($ctable); mysql_query("INSERT INTO {$r[0]} SELECT * FROM {$database}.{$r[0]}"); } ?> Untested... but should work (when run as a user with suitable permissions). :) -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.general (#170227) next »