PHP基于单例模式实现的数据库操作基类
发布:smiling 来源: PHP粉丝网 添加日期:2021-07-06 09:50:41 浏览: 评论:0
这篇文章主要介绍了PHP基于单例模式实现的数据库操作基类,涉及PHP操作数据库的基本配置与增删改查等操作技巧,需要的朋友可以参考下。
本文实例讲述了PHP基于单例模式实现的数据库操作基类,分享给大家供大家参考,具体如下:
配置文件:
- <?php
- $db = array(
- 'host'=>'localhost',
- 'user'=>'root',
- 'password'=>'',
- 'database'=>'test',
- )
- ?>
php 数据库基类:
- <?php
- class db {
- public $conn;
- public static $sql;
- public static $instance=null;
- private function __construct(){
- require_once('db.config.php');
- $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
- if(!mysql_select_db($db['database'],$this->conn)){
- echo "失败";
- };
- mysql_query('set names utf8',$this->conn);
- }
- public static function getInstance(){
- if(is_null(self::$instance)){
- self::$instance = new db;
- }
- return self::$instance;
- }
- /**
- * 查询数据库
- */
- public function select($table,$condition=array(),$field = array()){
- $where='';
- if(!emptyempty($condition)){
- foreach($condition as $k=>$v){
- $where.=$k."='".$v."' and ";
- }
- $where='where '.$where .'1=1';
- }
- $fieldstr = '';
- if(!emptyempty($field)){
- foreach($field as $k=>$v){
- $fieldstr.= $v.',';
- }
- $fieldstr = rtrim($fieldstr,',');
- }else{
- $fieldstr = '*';
- }
- self::$sql = "select {$fieldstr} from {$table} {$where}";
- $result=mysql_query(self::$sql,$this->conn);
- $resuleRow = array();
- $i = 0;
- while($row=mysql_fetch_assoc($result)){
- foreach($row as $k=>$v){
- $resuleRow[$i][$k] = $v;
- }
- $i++;
- }
- return $resuleRow;
- }
- /**
- * 添加一条记录
- */
- public function insert($table,$data){
- $values = '';
- $datas = '';
- foreach($data as $k=>$v){
- $values.=$k.',';
- $datas.="'$v'".',';
- }
- $values = rtrim($values,',');
- $datas = rtrim($datas,',');
- self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})";
- if(mysql_query(self::$sql)){
- return mysql_insert_id();
- }else{
- return false;
- };
- }
- /**
- * 修改一条记录
- */
- public function update($table,$data,$condition=array()){
- $where='';
- if(!emptyempty($condition)){
- foreach($condition as $k=>$v){
- $where.=$k."='".$v."' and ";
- }
- $where='where '.$where .'1=1';
- }
- $updatastr = '';
- if(!emptyempty($data)){
- foreach($data as $k=>$v){
- $updatastr.= $k."='".$v."',";
- }
- $updatastr = 'set '.rtrim($updatastr,',');
- }
- self::$sql = "update {$table} {$updatastr} {$where}";
- return mysql_query(self::$sql);
- }
- /**
- * 删除记录
- */
- public function delete($table,$condition){
- $where='';
- if(!emptyempty($condition)){
- foreach($condition as $k=>$v){
- $where.=$k."='".$v."' and ";
- }
- $where='where '.$where .'1=1';
- }
- self::$sql = "delete from {$table} {$where}";
- return mysql_query(self::$sql);
- }
- public static function getLastSql(){
- echo self::$sql;
- }
- }
- $db = db::getInstance();
- //$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));
- //echo $db->insert('demo',array('name'=>'脚本之家','password'=>'123'));
- //echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
- echo $db->delete('demo',array('id'=>'2'));
- db::getLastSql();
- echo "<pre>";
- ?>
Tags: PHP单例模式 PHP数据库类
- 上一篇:PHP基于单例模式实现的mysql类
- 下一篇:php实现的xml操作类
相关文章
- ·PHP基于单例模式实现的mysql类(2021-07-05)
- ·php实现的mongoDB单例模式操作类(2021-08-30)
- ·php数据库写入操作类(2014-09-11)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)