PHP同时连接多个mysql数据库示例代码

PHP同时连接多个mysql数据库示例代码,第1张

这篇文章主要介绍了PHP同时连接多个mysql数据库的具体实现,需要的朋友可以参考下

实例:

代码如下:

<?php

$conn1

=

mysql_connect("127.0.0.1",

"root","root","db1")

mysql_select_db("db1",

$conn1)

$conn2

=

mysql_connect("127.0.0.1",

"root","root","db2")

mysql_select_db("db2",

$conn2)

$sql

=

"select

*

from

ip"

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]."n"

$sql

=

"select

*

from

web

"

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]

?>

这段代码存在问题,在程序执行时会报错:PHP

Warning:

mysql_fetch_array()

expects

parameter

1

to

be

resource,

boolean

given

in

....

原因分析:

程序开始建立两个数据库链接,函数mysql_query()原型:

resource

mysql_query

(

string

$query

[,

resource

$link_identifier

]

)

向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定

link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用

mysql_connect()

函数来建立一个连接并使用之。查询结果会被缓存。

在本例中由于没有指定link_identifier,所以,在执行第一条sql时,默认使用的是上一个打开的链接,即$conn2,而实际上第一条sql语句应该使用的是$conn1,所以导致报错,所以为了能够链接多个mysql数据库,可以使用如下方法:

方法1:在mysql_query函数中指定所用连接,即:

代码如下:

<?php

$conn1

=

mysql_connect("127.0.0.1",

"root","root","db1")

mysql_select_db("Muma",

$conn1)

$conn2

=

mysql_connect("127.0.0.1",

"root","root","db2")

mysql_select_db("product",

$conn2)

$sql

=

"select

*

from

ip"

$query

=

mysql_query($sql,$conn1)

//添加连接$conn1

if($row

=

mysql_fetch_array($query))

echo

$row[0]."n"

$sql

=

"select

*

from

web

"

$query

=

mysql_query($sql,

$conn2)

if($row

=

mysql_fetch_array($query))

echo

$row[0]

?>

方法2:在sql语句中关联所用数据库,此时可以省略mysql_query的第二个参数,即:

代码如下:

<?php

$conn1

=

mysql_connect("127.0.0.1",

"root","root","db1")

mysql_select_db("db1",

$conn1)

$conn2

=

mysql_connect("127.0.0.1",

"root","root","db2")

mysql_select_db("db2",

$conn2)

$sql

=

"select

*

from

db1.ip"

//关联数据库

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]."n"

$sql

=

"select

*

from

db2.web

"

$query

=

mysql_query($sql)

if($row

=

mysql_fetch_array($query))

echo

$row[0]

?>

可以的,PB中同时连接多个数据库,如连接SQLServer2000和Oracle8 ,代码如下:

string ls_startupfile

ls_startupfile='hisini.ini'

sqlca.DBMS = ProfileString(ls_startupfile, "database", "dbms", "")

sqlca.database = ProfileString(ls_startupfile, "database", "database", "")

sqlca.userid = ProfileString(ls_startupfile, "database", "userid", "")

sqlca.dbpass = ProfileString(ls_startupfile, "database", "dbpass", "")

sqlca.logid = ProfileString(ls_startupfile, "database", "logid", "")

sqlca.logpass = ProfileString(ls_startupfile, "database", "LogPassWord", "")

sqlca.servername = ProfileString(ls_startupfile, "database", "servername", "")

sqlca.dbparm = ProfileString(ls_startupfile, "database", "dbparm", "")

remote_trans= CREATE transaction

remote_trans.DBMS = ProfileString(ls_startupfile, "Database_remote", "dbms", "")

remote_trans.database = ProfileString(ls_startupfile, "Database_remote", "database", "")

remote_trans.userid = ProfileString(ls_startupfile, "database_remote", "userid", "")

remote_trans.dbpass = ProfileString(ls_startupfile, "database_remote", "dbpass", "")

remote_trans.logid = ProfileString(ls_startupfile, "database_remote", "logid", "")

remote_trans.logpass = ProfileString(ls_startupfile, "database_remote", "LogPassWord", "")

remote_trans.servername = ProfileString(ls_startupfile, "database_remote", "servername", "")

remote_trans.dbparm = ProfileString(ls_startupfile, "database_remote", "dbparm", "")

//附hisini.ini

[Database]

DBMS=MSS Microsoft SQL Server 6.x

Database=his

UserId=

DatabasePassword=

ServerName=.

LogId=sa

Lock=

Prompt=0

computer='11'

ocx= 0

use0='之住院管理'

cfprint='1'

[Database_remote]

DBMS = "O84 Oracle8/8i(8.x.4+)"

ServerName = "oracle8"

LogId = "dba"

Database=zx

UserId=

DatabasePassword=

Lock=

Prompt=0

computer='11'

ocx= 0

cfprint='1'

$conn1=mysql_connect('...','...','...') //将第一个数据库连接资源保存到变量conn1中

$conn2=mysql_connect('...','...','...') //将第二个数据库连接资源保存到变量conn2中

然后在执行查询 *** 作时,指定使用哪个连接资源,如:mysql_query('select ....',$conn1)

希望可以帮到你,谢谢!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9366998.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存