天天看點

php mysql連接配接多個庫_php如何同時連接配接多個資料庫

下面是一個函數能夠保證連接配接多個資料庫的下不同的表的函數,可以收藏一下,比較實用,測試過是有用的。

function mysql_oper($oper,$db,$table,$where='1',$limit='10'){

$conn=mysql_connect('localhost','like','admin',true) or mysql_error();

mysql_select_db($db,$conn);

mysql_query("set names utf8");//必須和表單資料字元保持一緻

if($oper=='select'){

$query="select * from `$table` where ".$where." limit ".$limit;

$res=mysql_query($query,$conn) or die("擷取資料失敗");

$arr=array();

while($row=mysql_fetch_row($res)){

$arr[]=$row;

}

mysql_close($conn);

return $arr;

}

}

預設是查詢操作,你可以根據你自己的要求修改這段代碼!,就是那個$oper操作符作用下的代碼

例如我現在要查詢名為test的資料庫下的shop資料表,并且id在10到12的資料,那麼我就像下面這樣去調用就可以了

mysql_oper('select','test','shop','id>=10 and id<=12');