I need to do this in PHP only, can't use PhpMyAdmin because the process needs to be automated.
I've been using the following code to copy the database and its tables in to a new database with its new tables, with no change to table names, just change to database name.
Seems to work when the tables have 10 or so fields in them but I just added a table with 30-40 fields in it and it seems to fall down -as in, it doesn't copy the database table and it's table rows into the new database. Is there some error checking or buffering problems that I might be running into, or is it some other problem? Doesn't output a PDO exception error either.
here's the code:
$GLOBALS['MySQL_Host'] = "127.0.0.1";
$GLOBALS['MySQL_Username'] = "username";
$GLOBALS['MySQL_Password'] = "password";
$ExistingDatabase = "database_1";
$NewDatabase = "database_2";
// connect server 1
$dblink1 = mysqli_connect("{$GLOBALS['MySQL_Host']}", "{$GLOBALS['MySQL_Username']}", "
{$GLOBALS['MySQL_Password']}", "{$ExistingDatabase}");
// select database_1
mysqli_select_db($dblink1,"$ExistingDatabase");
// get tables from database_1
$tables = mysqli_fetch_all(mysqli_query($dblink1, "SHOW TABLES FROM $ExistingDatabase"));
// connect server 2
$dblink2 = mysqli_connect("{$GLOBALS['MySQL_Host']}", "{$GLOBALS['MySQL_Username']}",
"{$GLOBALS['MySQL_Password']}");
// select database 2
mysqli_select_db($dblink2, "{$NewDatabase}");
$n=0;
foreach($tables as $table){
// get structure from table on server 1
$tableinfo = mysqli_fetch_array(mysqli_query($dblink1,"SHOW CREATE TABLE $table[0]"));
// use found structure to make table on server 2
mysqli_query($dblink2," $tableinfo[1] ");
// select all content
$result = mysqli_query($dblink1,"SELECT * FROM $table[0] ");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
mysqli_query($dblink2,"INSERT INTO $table[0] (".implode(", ",array_keys($row)).") VALUES
('".implode("', '",array_values($row))."')");
}
$n++;
}
mysqli_close($dblink1);
mysqli_close($dblink2);
I tried many (like 10) code snippets from stackoverflow and this was the only one I could get working. Is this a good approach? If not, what is? If so, how can I fix it to make sure it's more robust and actually works?
Remember I can't use PhpMyAdmin. Please don't close this post and link a PhpMyAdmin solution. Thanks.
Edit
I've tried the following commands and executions without success. Here's the top of the file, and the 4 trials I've tried are below it:
$GLOBALS['Dir'] = "C:/wamp64/www/projectsite/";
$dumpDir = $GLOBALS['Dir'].'Databases/';
$exisitingDatabase = "exisitingDatabaseName";
$newDatabase = "newDatabaseName";
if(!file_exists($dumpDir)){
mkdir($dumpDir);
}
$Dbh = new PDO("mysql:host=127.0.0.1:3307", $GLOBALS['MySQL_Username'],
$GLOBALS['MySQL_Password']);
1.
$Dbh->exec("mysqldump --tab={$Dumpdir} {$ExistingDatabase}");
2
$Dbh->exec("-h {$GLOBALS['MySQL_Host']} -u {$GLOBALS['MySQL_Username']} --password={$GLOBALS['MySQL_Password']} > {$Dir_and_Filename}.sql");
3
$cmd = "mysqldump -h {$GLOBALS['MySQL_Host']} -u {$GLOBALS['MySQL_Username']} --password={$GLOBALS['MySQL_Password']} {$existingDatabase} > {$newDatabase}.sql";
exec($cmd);
4
exec("mysqldump -u {$GLOBALS['MySQL_Username']} -p {$ExistingDatabase} > {$NewDatabase}.sql");
Edit
Managed to get this dumping an sql file so far:
$return_var = NULL;
$output = NULL;
$command = "$Dumpdir/mysqldump -u {$GLOBALS['MySQL_Username']} -h {$GLOBALS['MySQL_Host']} -p{$GLOBALS['MySQL_Password']} {$ExistingDatabase} > $Dumpdir/{$NewDatabase}.sql";
if(exec($command, $output, $return_var)){
echo "asd";
}else{
echo "qwe";
}