2

I am using the standard MySQL functions that PHP has built in for copying one table to an new table. This works perfect when the tables are in the same database. But I want to copy it to another databasename with same user and password.

Any suggestions how to achive this? (Since $database can only contain 1 databasename)

Error shown is Table 'torzahttp_rsw.torzahttp_rsw.kwaliteit' doesn't exist torzahttp_rswis the database name, and kwaliteit the table name. Why is the databasename used twice?

// Create a new MySQL database connection
if (!$con = new mysqli('localhost', $username, $password, $database)) {
    die('An error occurred while connecting to the MySQL server!<br><br>' . $con->connect_error);
}

// Create an array of MySQL queries to run
$sql = array(
    'DROP TABLE IF EXISTS `backup_db.backup_table`;',
    'CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`'
);

// Run the MySQL queries
if (sizeof($sql) > 0) {
    foreach ($sql as $query) {
        if (!$con->query($query)) {
            die('A MySQL error has occurred!<br><br>' . $con->error);
        }
    }
}

$con->close();
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Muiter
  • 1,470
  • 6
  • 26
  • 39
  • Do you get any messages/errors? – Nigel Ren Jan 28 '20 at 07:11
  • 2
    Although you connect to a database, you can still access other databases using the method you currently do ( `databasename.tablename` ) as long as the user you connect as has the rights to that database and the database is on the same server (AFAIK) – Nigel Ren Jan 28 '20 at 07:14
  • Does this answer your question? [How to copy table to another database](https://stackoverflow.com/questions/3932608/how-to-copy-a-table-from-one-mysql-database-to-another-mysql-database) – Daniel Protopopov Jan 28 '20 at 07:53
  • @DanielProtopopov no it does not. I need an php script to copy certain tables. – Muiter Jan 28 '20 at 08:11
  • @NigelRen now I was able to see an error. – Muiter Jan 28 '20 at 08:17

2 Answers2

1

From the MySQL manual:

If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.

Your code:

$sql = array(
    'DROP TABLE IF EXISTS `backup_db.backup_table`;',
    'CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`'
);

should look like this:

$sql = array(
    'DROP TABLE IF EXISTS `backup_db`.`backup_table`;',
    'CREATE TABLE `backup_db`.`backup_table` SELECT * FROM `live_db`.`live_table`'
);

Or, just drop the backticks altogether.

miken32
  • 42,008
  • 16
  • 111
  • 154
0

I have used this solution to create an mysql dump first and then processioning it.

//DB 
$user = 'xxxxx';
$password = 'xxxxx';
$host = 'localhost';
$database_old = 'old_db';
$database_new = 'new_db';

$table = 'kwaliteit';
$file = 'kwaliteit.sql';

exec('mysqldump --user='.$user.' --password='.$password.' --host='.$host.' '.$database_old.' '.$table.' > uploads/'.$file);

exec('mysql --user='.$user.' --password='.$password.' --host='.$host.' '.$database_new.' < uploads/'.$file);

unlink('uploads/'.$file);
Muiter
  • 1,470
  • 6
  • 26
  • 39