$conn1=mysql_connect('...','...','...'); //將第一個(gè)數(shù)據(jù)庫連接資源保存到變量conn1中

成都創(chuàng)新互聯(lián)公司2013年成立,先為臨沭等服務(wù)建站,臨沭等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為臨沭企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
$conn2=mysql_connect('...','...','...'); //將第二個(gè)數(shù)據(jù)庫連接資源保存到變量conn2中
然后在執(zhí)行查詢操作時(shí),指定使用哪個(gè)連接資源,如:mysql_query('select ....',$conn1);
希望可以幫到你,謝謝!
?php
$link_A=mysql_connect("localhost:917","root","skcg1024",true);
mysql_select_db("db_a",$link_A);
$link_B=mysql_connect("localhost:917","root","skcg1024",true);
mysql_select_db("db_a",$link_B);
print_r($link_A);
print_r($link_B);
mysql_connect的第四個(gè)參數(shù)設(shè)置成true,表示創(chuàng)建一個(gè)新的連接
這樣你就獲得咯兩個(gè)數(shù)據(jù)庫連接,然后指定對應(yīng)的數(shù)據(jù)庫即可。但不建議這么做
建議采用數(shù)據(jù)庫.數(shù)據(jù)表的格式?訪問存儲數(shù)據(jù),代碼簡練,邏輯清楚
實(shí)例化兩條sql鏈接.
例如?
$wdb?=?mysql_connect('localhost','root','123456','a1');//負(fù)責(zé)寫入的數(shù)據(jù)庫
$rdb?=?mysql_connect('192.168.xx.xx','root','123456','a2');//負(fù)責(zé)讀的數(shù)據(jù)庫
這樣就可以鏈接兩個(gè)數(shù)據(jù)庫了。
只能連接一個(gè)庫,庫里面可以有多張表。
我是弄IOS的,所以在這些方面我比較熟。
這篇文章主要介紹了PHP同時(shí)連接多個(gè)mysql數(shù)據(jù)庫的具體實(shí)現(xiàn),需要的朋友可以參考下
實(shí)例:
代碼如下:
?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];
?
這段代碼存在問題,在程序執(zhí)行時(shí)會報(bào)錯(cuò):PHP
Warning:
mysql_fetch_array()
expects
parameter
1
to
be
resource,
boolean
given
in
....
原因分析:
程序開始建立兩個(gè)數(shù)據(jù)庫鏈接,函數(shù)mysql_query()原型:
resource
mysql_query
(
string
$query
[,
resource
$link_identifier
]
)
向與指定的連接標(biāo)識符關(guān)聯(lián)的服務(wù)器中的當(dāng)前活動數(shù)據(jù)庫發(fā)送一條查詢。如果沒有指定
link_identifier,則使用上一個(gè)打開的連接。如果沒有打開的連接,本函數(shù)會嘗試無參數(shù)調(diào)用
mysql_connect()
函數(shù)來建立一個(gè)連接并使用之。查詢結(jié)果會被緩存。
在本例中由于沒有指定link_identifier,所以,在執(zhí)行第一條sql時(shí),默認(rèn)使用的是上一個(gè)打開的鏈接,即$conn2,而實(shí)際上第一條sql語句應(yīng)該使用的是$conn1,所以導(dǎo)致報(bào)錯(cuò),所以為了能夠鏈接多個(gè)mysql數(shù)據(jù)庫,可以使用如下方法:
方法1:在mysql_query函數(shù)中指定所用連接,即:
代碼如下:
?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語句中關(guān)聯(lián)所用數(shù)據(jù)庫,此時(shí)可以省略mysql_query的第二個(gè)參數(shù),即:
代碼如下:
?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";
//關(guān)聯(lián)數(shù)據(jù)庫
$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];
?
網(wǎng)站標(biāo)題:php同時(shí)訪問數(shù)據(jù)庫的簡單介紹
新聞來源:http://chinadenli.net/article0/dsgsjio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、微信公眾號、網(wǎng)站策劃、動態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)