config.php文件中配置的数据库是test1,另一个数据库是test2,代码如下
//双数据库增加数据 $User = M("User1"); // 实例化User1对象 $data['username'] = 'ThinkPHP'; $data['passwd'] = 'ThinkPdm'; echo $User->add($data); $db_dsn = "mysql://root:123123@localhost:3306/test2"; $db = new Db($db_dsn); //另一个数据的连接 $db = Db::getInstance($db_dsn); echo $db->execute("INSERT INTO `test2`.`think_user1` (`id` ,`username` ,`passwd`) VALUES ('', 'sdf', 'sss')"); //$User->data($data)->add(); //使用data方法连贯操作
//双数据库删除数据 $User = M("User1"); // 实例化User对象 echo $User->where('id=8')->delete(); $db_dsn = "mysql://root:123123@localhost:3306/test2"; $db = new Db($db_dsn); //另一个数据的连接 $db = Db::getInstance($db_dsn); echo $db->execute("DELETE FROM `test2`.`think_user1` WHERE `think_user1`.`id` = 8"); ////双数据库修改数据 $User = M("User1"); // 实例化User对象 $data['username'] = 'Thi111c'; $data['passwd'] = 'Think111c'; echo $User->where('id=7')->save($data); $db_dsn = "mysql://root:123123@localhost:3306/test2"; $db = new Db($db_dsn); //另一个数据的连接 $db = Db::getInstance($db_dsn); echo $db->execute("UPDATE `test2`.`think_user1` SET `username` = 'Thin222c',`passwd` = 'sssc' WHERE `think_user1`.`id` =7"); ////双数据库查询数据 $user = M("User"); //默认的数据连接 $list = $user->where('id=1')->find(); echo "ddddddddd<br>"; dump($list); $db_dsn = "mysql://root:123123@localhost:3306/test2"; $db = new Db($db_dsn); //另一个数据的连接 $db = Db::getInstance($db_dsn); $aaa = $db->query("select * from think_user where id = 1"); dump($aaa);
/× thnkphp2.1 addConnect 方式,addConnect方式在2.2中 会提示Model:addConnect您所请求的方法不存在!因为2.2版本已经删除不了addConnect方法
$myConnect1 = 'mysql://root:123123@localhost:3306/test2'; //$obj=new Model("IndexModel"); $obj = M('User','AdvModel'); $obj->addConnect($myConnect1,1); $obj->switchConnect(0); $result = $obj->query("select * from think_user where id = 1"); dump($result);
*/
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END