views:

65

answers:

2

Hi, I need to copy a table from one database to another. This will be a cronjob. Which one is the best way to do it? PHP script or Shell Script. The problem with PHP, both databases has different usernames and passwords so I can't do it like this.

CREATE TABLE db1.table1 SELECT * FROM db2.table1

Should I just connect first DB get all records and insert all to new database using WHILE loop or there is a better way?

I prefer a shell script to do this instead of PHP script.

Thanks

+4  A: 

I'd dump it. Much less complicated than anything PHP based.

mysqldump -u user1 -ppassword1 databasename > dump.sql
mysql -u user2 -ppassword2 databasename < dump.sql

MySQL reference: 4.5.4. mysqldump — A Database Backup Program

Pekka
Since I want to transfer only one table, I guess with a little bit mod this will workmysqldump -u user1 -ppassword1 --add-drop-table databasename tablename > dump.sqlthank you
Ergec
If you use phpmyadmin, then it will be better.
lakum4stackof
A: 

Phpmyadmin has inbuilt functionality to copy tables from one database to another. Otherwise you can go with Pekka or export table then import table.

lakum4stackof
@lakum IIRC, the inbuilt functionality will work only if the same user has access to both databases.
Pekka