這篇文章主要介紹了PHP同時(shí)連接多個(gè)mysql數(shù)據(jù)庫(kù)的具體實(shí)現(xiàn),需要的朋友可以參考下
創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元大柴旦做網(wǎng)站,已為上家服務(wù),為大柴旦各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
實(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];
?
這段代碼存在問(wèn)題,在程序執(zhí)行時(shí)會(huì)報(bào)錯(cuò):PHP
Warning:
mysql_fetch_array()
expects
parameter
1
to
be
resource,
boolean
given
in
....
原因分析:
程序開(kāi)始建立兩個(gè)數(shù)據(jù)庫(kù)鏈接,函數(shù)mysql_query()原型:
resource
mysql_query
(
string
$query
[,
resource
$link_identifier
]
)
向與指定的連接標(biāo)識(shí)符關(guān)聯(lián)的服務(wù)器中的當(dāng)前活動(dòng)數(shù)據(jù)庫(kù)發(fā)送一條查詢。如果沒(méi)有指定
link_identifier,則使用上一個(gè)打開(kāi)的連接。如果沒(méi)有打開(kāi)的連接,本函數(shù)會(huì)嘗試無(wú)參數(shù)調(diào)用
mysql_connect()
函數(shù)來(lái)建立一個(gè)連接并使用之。查詢結(jié)果會(huì)被緩存。
在本例中由于沒(méi)有指定link_identifier,所以,在執(zhí)行第一條sql時(shí),默認(rèn)使用的是上一個(gè)打開(kāi)的鏈接,即$conn2,而實(shí)際上第一條sql語(yǔ)句應(yīng)該使用的是$conn1,所以導(dǎo)致報(bào)錯(cuò),所以為了能夠鏈接多個(gè)mysql數(shù)據(jù)庫(kù),可以使用如下方法:
方法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語(yǔ)句中關(guān)聯(lián)所用數(shù)據(jù)庫(kù),此時(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ù)庫(kù)
$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];
?
我感覺(jué)你可以把其他兩個(gè)數(shù)據(jù)庫(kù)中的表提取出來(lái),全都放進(jìn)一個(gè)數(shù)據(jù)庫(kù)中,這樣在添加的時(shí)候不就容易了嗎?而且也不會(huì)帶來(lái)其他的影響。我以前做的一個(gè)一個(gè)后臺(tái)同時(shí)管理三個(gè)網(wǎng)站的程序時(shí)就是采用的這種方法。沒(méi)有必要非要去連三個(gè)數(shù)據(jù)庫(kù)的。
$conn1=mysql_connect('...','...','...'); //將第一個(gè)數(shù)據(jù)庫(kù)連接資源保存到變量conn1中
$conn2=mysql_connect('...','...','...'); //將第二個(gè)數(shù)據(jù)庫(kù)連接資源保存到變量conn2中
然后在執(zhí)行查詢操作時(shí),指定使用哪個(gè)連接資源,如:mysql_query('select ....',$conn1);
希望可以幫到你,謝謝!
實(shí)例化兩條sql鏈接.例如 $wdb = mysql_connect('localhost','root','123456','a1');//負(fù)責(zé)寫(xiě)入的數(shù)據(jù)庫(kù)$rdb = mysql_connect('192.168.xx.xx','root','123456','a2');//負(fù)責(zé)讀的數(shù)據(jù)庫(kù)這樣就可以鏈接兩個(gè)數(shù)據(jù)庫(kù)了。
個(gè)人建議采集到的數(shù)據(jù)存儲(chǔ)為二維數(shù)組,其中商品id是唯一的,所以將id作為鍵值,然后每個(gè)鍵值對(duì)應(yīng)的是一個(gè)一次包含title,price等數(shù)據(jù)的二維數(shù)組,這樣采集完成后,可以將這個(gè)二維數(shù)組遍歷循環(huán)插入數(shù)據(jù)庫(kù),這樣也不容易出現(xiàn)錯(cuò)誤
比如其中一個(gè)商品id為1,標(biāo)題為“牙刷”,價(jià)格為$2,就這樣寫(xiě)入數(shù)組$arr[1]=array("牙刷","$2")
$sql="insert into news(catid,huan,title,publisher,img,keywords,copyfrom,description,hit,hot,sort,addtime)values('".$catid."','".$huan."','".$title."','".$publisher."','".$img."','".$keywords."','".$copyfrom."','".$description."','".$hit."','".$hot."',".$sort."','".$addtime."')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$sql2="insert into show(catid,huan,title,publisher,img,keywords,copyfrom,description,hit,hot,sort,addtime)values('".$catid."','".$huan."','".$title."','".$publisher."','".$img."','".$keywords."','".$copyfrom."','".$description."','".$hit."','".$hot."',".$sort."','".$addtime."')";
mysql_query($sql2,$con);
echo "成功添加!";mysql_close($con)
當(dāng)前標(biāo)題:php寫(xiě)入多個(gè)數(shù)據(jù)庫(kù),php連接多個(gè)數(shù)據(jù)庫(kù)
瀏覽地址:http://chinadenli.net/article20/dsicpco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、App設(shè)計(jì)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容