php连接,查询mysql数据库的几种方法。
17-02-12 13:39
字数 6982
阅读 3663
已编辑
本文简单介绍了php的三种查询mysql数据库的方式,非常简单,适合初学者。
connType=$connType;
$this->dbHost=$dbHost;
$this->dbName=$dbName;
$this->dbUser=$dbUser;
$this->dbPwd=$dbPwd;
$this->charset=$charset;
}
/*
* @查询结果数组(全部结果集)
* @param1 sql [string]要执行的sql语句
* @param2 resultType [string]返回的数组类型 (assoc|idxarr)
*/
public function getList($sql,$resultType){
$sql=htmlspecialchars($sql);
if($this->connType=='mysql'){//mysql链接数据库
$conn=mysql_connect($this->dbHost,$this->dbUser,$this->dbPwd);
if(!$conn){
die(mysql_error());
}else{
echo $this->succInfo;
if(mysql_select_db($this->dbName) && mysql_set_charset($this->charset)){
$query=mysql_query($sql,$conn);
$resArr=array();
if($resultType=='assoc'){
while(($rs=mysql_fetch_assoc($query))!==false){
$resArr[]=$rs;
}
}elseif($resultType=='idxarr'){
while(($rs=mysql_fetch_row($query))!==false){
$resArr[]=$rs;
}
}
}
if(isset($resArr)) return $resArr;
else exit('请检查数据库名称或字符集设置是否正确');
}
}elseif($this->connType=='mysqli'){//mysqli链接数据库
$conn=new mysqli($this->dbHost,$this->dbUser,$this->dbPwd);
if(!$conn){
die(mysqli_connect_error());
}else{
echo $this->succInfo;
if($conn->set_charset($this->charset) && $conn->select_db($this->dbName)){
$query=$conn->query($sql);
//此处和mysql有差异
$resArr=array();
if($resultType=='assoc'){
while($rs=$query->fetch_assoc()){
$resArr[]=$rs;
}
}elseif($resultType=='idxarr'){
while($rs=$query->fetch_row()){
$resArr[]=$rs;
}
}
}
if(isset($resArr)) return $resArr;
else exit("请检查数据库名称或字符集设置是否正确");
}
}elseif($this->connType=='pdo'){//pdo链接数据库
$dsn="mysql:host=$this->dbHost;dbname=$this->dbName";
$conn=new PDO($dsn,$this->dbUser,$this->dbPwd);
if(!$conn){
var_dump($conn->errorInfo());
}else{
echo $this->succInfo;
if($conn->query("set names $this->charset")){
//预处理
/*$rs = $conn->prepare("SELECT * FROM USER");
$rs->execute();
while($row = $rs->fetch()){
print_r($row);
}*/
$query=$conn->query($sql);
if($resultType=='assoc'){
$query->setFetchMode(PDO::FETCH_ASSOC);
}elseif($resultType=='idxarr'){
$query->setFetchMode(PDO::FETCH_NUM);
}
$resArr=$query->fetchAll();
}
if(isset($resArr)) return $resArr;
else exit('请检查字符集设置是否正确');
}
}
}
}
//demo
$res=new ms_conn('mysql','127.0.0.1','bbs','root','','utf8');
var_dump($res->getList('select * from USER WHERE 1=1','assoc'));
connType=$connType;
$this->dbHost=$dbHost;
$this->dbName=$dbName;
$this->dbUser=$dbUser;
$this->dbPwd=$dbPwd;
$this->charset=$charset;
}
/*
* @查询结果数组(全部结果集)
* @param1 sql [string]要执行的sql语句
* @param2 resultType [string]返回的数组类型 (assoc|idxarr)
*/
public function getList($sql,$resultType){
$sql=htmlspecialchars($sql);
if($this->connType=='mysql'){//mysql链接数据库
$conn=mysql_connect($this->dbHost,$this->dbUser,$this->dbPwd);
if(!$conn){
die(mysql_error());
}else{
echo $this->succInfo;
if(mysql_select_db($this->dbName) && mysql_set_charset($this->charset)){
$query=mysql_query($sql,$conn);
$resArr=array();
if($resultType=='assoc'){
while(($rs=mysql_fetch_assoc($query))!==false){
$resArr[]=$rs;
}
}elseif($resultType=='idxarr'){
while(($rs=mysql_fetch_row($query))!==false){
$resArr[]=$rs;
}
}
}
if(isset($resArr)) return $resArr;
else exit('请检查数据库名称或字符集设置是否正确');
}
}elseif($this->connType=='mysqli'){//mysqli链接数据库
$conn=new mysqli($this->dbHost,$this->dbUser,$this->dbPwd);
if(!$conn){
die(mysqli_connect_error());
}else{
echo $this->succInfo;
if($conn->set_charset($this->charset) && $conn->select_db($this->dbName)){
$query=$conn->query($sql);
//此处和mysql有差异
$resArr=array();
if($resultType=='assoc'){
while($rs=$query->fetch_assoc()){
$resArr[]=$rs;
}
}elseif($resultType=='idxarr'){
while($rs=$query->fetch_row()){
$resArr[]=$rs;
}
}
}
if(isset($resArr)) return $resArr;
else exit("请检查数据库名称或字符集设置是否正确");
}
}elseif($this->connType=='pdo'){//pdo链接数据库
$dsn="mysql:host=$this->dbHost;dbname=$this->dbName";
$conn=new PDO($dsn,$this->dbUser,$this->dbPwd);
if(!$conn){
var_dump($conn->errorInfo());
}else{
echo $this->succInfo;
if($conn->query("set names $this->charset")){
//预处理
/*$rs = $conn->prepare("SELECT * FROM USER");
$rs->execute();
while($row = $rs->fetch()){
print_r($row);
}*/
$query=$conn->query($sql);
if($resultType=='assoc'){
$query->setFetchMode(PDO::FETCH_ASSOC);
}elseif($resultType=='idxarr'){
$query->setFetchMode(PDO::FETCH_NUM);
}
$resArr=$query->fetchAll();
}
if(isset($resArr)) return $resArr;
else exit('请检查字符集设置是否正确');
}
}
}
}
//demo
$res=new ms_conn('mysql','127.0.0.1','test','root','','utf8');
var_dump($res->getList('select * from USER WHERE 1=1','assoc'));
0人点赞>
0 条评论
排序方式
时间
投票
快来抢占一楼吧
请登录后发表评论
相关推荐
文章归档
最新文章
最受欢迎
18-11-07 08:59
18-07-25 19:30
18-04-27 10:05
昨天 16:33
17-10-12 13:41