Move table from one database to another in MySQL

PhpMysql

Php Problem Overview


How to move a table from one Database to another Database without using phpMyAdmin? It will be better if it is possible by PHP.

Php Solutions


Solution 1 - Php

ALTER TABLE .. can be used to move tables from one database to another:

alter table my_old_db.mytable rename my_new_db.mytable

Warning: as you asked, this is a move, not a copy to the new database!

But you will keep table data (and not integrity constraints if they apply in your case)

Regarding php, php is able to run sql commands so it won't be a problem.

Solution 2 - Php

Entire Database (all tables):

mysqldump -u root databasename > dump.sql
mysql -u root databasename < dump.sql

One Table:

mysqldump -u root -p yourpass dbname tablename | mysql -u root -p pass secondDB

PHP:

Run PHP SELECT FROM SOURCE-DB TABLE and Run INSERT INTO Table IN TARGET-DB

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSarvap PraharanayuthanView Question on Stackoverflow
Solution 1 - Phpuser2196728View Answer on Stackoverflow
Solution 2 - PhpGGioView Answer on Stackoverflow