The following example shows how to copy data from one database to another. 1, in config. configure useful database information in php // source database & quot; DB_DSN1 & quot; & gt; & quot; mysql ://...
The following example shows how to copy data from one database to another.
1. configure the useful database information in config. php.
- // Source database
- "DB_DSN1" => "mysql: // user name: password @ database Address: Port/database name ",
- // Target database
- "DB_DSN2" => "mysql: // user name: password @ database Address: Port/database name ",
- // Example:
- "DB_DSN1" => "mysql: // user: 123@127.0.0.1: 3306/db1 ",
2. usage
1) retrieve configuration information from config. php
$ Dsn1 = C ('Db _ DSN1 ');
$ Dsn2 = C ("DB_DSN2 ");
2) define a model
$ Model = new Model ();
$ Model-> db ("1", $ dsn1); // define the model as dsn1 by default.
3) define the source data table to be queried
$ Table = $ model-> db ("1")-> table ('name of the queried table ');
$ Data = $ table-> where (query condition)-> select ();
4) store the source database data to the target data table
- Foreach ($ data as $ val ){
- $ Model-> db ("2", $ dsn2)-> query ("SET FOREIGN_KEY_CHECKS = 0 ;");
- // Save
- $ Rev = $ model-> db ("2", $ dsn2)-> table ($ table)-> where ($ mWhere)-> save ($ val );
- // Add
- // $ Rev = $ model-> db ("2", $ dsn2)-> table ($ table)-> add ($ val );
- }
- // The operation above directs the database to the target database again
- // You can also use this query $ model-> db ("2", $ dsn2)-> query ("SET FOREIGN_KEY_CHECKS = 0 ;");
The preceding four steps allow different databases to switch back and forth.