Home >Backend Development >PHP Tutorial >How to connect multiple databases in php at the same time_PHP tutorial
The following is a function that can ensure the connection of different tables in multiple databases. You can save it. It is more practical and useful after testing.
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; } }
The default is query operation, you can modify this code according to your own requirements! , which is the code under the action of the $oper operator
For example, if I now want to query the shop data table under the database named test, and the data with IDs between 10 and 12, then I can call it like the following
mysql_oper('select','test','shop','id>=10 and id