php简单数据库操作类的封装
本文实例为大家分享了php封装一个简单的数据库操作类,供大家参考,具体内容如下
一、代码
<?php class ConnDB{ private $local; private $username; private $pwd; private $db; private $connID; private $code; public function __construct($local,$username,$pwd,$connID,$code,$db){ $this->local=$local; $this->username=$username; $this->pwd=$pwd; $this->connID=$connID; $this->code=$code; $this->db=$db; $this->connect(); } public function connect(){ if($this->connID=="pconn"){ $this->connID=mysql_pconnect($this->local,$this->username,$this->pwd)or die("Connect MySQL False"); }else{ $this->connID=mysql_connect($this->local,$this->username,$this->pwd)or die("Connect MySQL False"); } mysql_select_db($this->db,$this->connID) or die("Connect DB False"); mysql_query("SET NAMES $this->code"); return $this->connID; //返回数据库连接对象 } } class AdminDB{ function executeSQL ($sql, $connID){ $sqlType = strtolower(substr(trim($sql), 0, 6)); //提取SQL语句的类型 $rs = mysql_query($sql,$connID); //执行SQL语句 if ($sqlType == 'select') { //如果是select查询 $arrayData = mysql_fetch_array($rs); //返回查询记录集 if (count($arrayData) == 0 || $rs == false) { //如果没查询到或发生错误 return false; //返回false } else { //否则 return $arrayData; //返回记录集 } } elseif ($sqlType == 'insert' || $sqlType == 'update' || $sqlType == 'delete') { //如果执行插入、更新或删除语句 return $rs; //返回语句执行状态,即成功返回true,失败返回false } else { return false; //如果不是上述查询,则返回false } } } $connobj=new ConnDB("localhost","root","root","conn","utf8","db_database20"); //实例化数据库连接类 $conn=$connobj->connect(); //返回连接标识 $admindb=new AdminDB(); //数据库操作类实例化 $res=$admindb->executeSQL ("select * from tb_demo01",$conn); //调用数据库操作类中方法执行查询语句 if($res){ //如果返回结果为真,则输出数据 print_r($res); //输出一个数组。 } ?>
二、运行结果
Array ( [0] => 1 [id] => 1 [1] => php新书上市 [title] => php新书上市 [2] => php新书上市啦,希望大家给出宝贵意见 [content] => php新书上市啦,希望大家给出宝贵意见 [3] => 2010-10-23 [date] => 2010-10-23 )
相关推荐
聪聪李 2020-09-10
WasteLand 2020-09-15
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28