想pdo同时操作两个数据库,只能做两个pdo的连线吗

WBOY
Release: 2016-06-23 13:55:41
Original
1005 people have browsed it

因为情况是这样
主站是个CMS,, 有文章, 会员等等

但有时候会有非常多不同的分站(当作是专题吧)
而每个分站的内容架构也不一样,所以不能用同一张数据表完成,也就可能每个分站有7~8个数据库


50个分站的话就已经要用到  300~400多张表?

所以才想分开数据库处理

$pdo = new PDO('mysql:host=localhost;dbname=cms', 'user','password');
$pdo_zt15 = new PDO('mysql:host=localhost;dbname=zt15', 'user','password');

同时操作两个,是因为分站(题专)也需要用到主站数据库的会员资料

我想同时操作数据库,只能这样做了吗?

还是有其他方法?


回复讨论(解决方案)

如果两个库在同一个mysql服务器上,且用户名及口令相同,那么可以在访问时在表名前缀库名:库名.表名

如果在同一部server,且???用?名,密?一?。
可以用select * from DB1.table1,DB2.table2; ??。
不用?建?次?接。

两位的意思是....

比如在分站专题时...

取得主站用户资料:select * from cms.user

然后用这样获取自己专题的资料:select * from zt15.product

这样只要一个$pdo就行了吧?

但是
在PDO??中
已?填上了?一?:dbname=cms  
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'user','password');


在sql?句再改用 DB1.table1 也可以?

但是
在PDO??中
已?填上了?一?:dbname=cms  
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'user','password');


在sql?句再改用 DB1.table1 也可以?



只要是同一台服务器,相同帐号密码,且帐号有访问两个库的权限。就可以了。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!