Home > php教程 > php手册 > php如何同时连接多个数据库

php如何同时连接多个数据库

WBOY
Release: 2016-06-13 10:18:59
Original
1015 people have browsed it

下面是一个函数能够保证连接多个数据库的下不同的表的函数,可以收藏一下,比较实用,测试过是有用的。

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;
		}
	}
Copy after login


默认是查询操作,你可以根据你自己的要求修改这段代码!,就是那个$oper操作符作用下的代码

例如我现在要查询名为test的数据库下的shop数据表,并且id在10到12的数据,那么我就像下面这样去调用就可以了

mysql_oper('select','test','shop','id>=10 and id



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template