欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP单例模式简单用法示例

程序员文章站 2024-03-11 11:50:13
本文实例讲述了php单例模式用法。分享给大家供大家参考,具体如下:

本文实例讲述了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 self();
  }
    return self::$instance;
  }
  /**
  * 查询数据库
  */
  public function select($table,$condition=array(),$field = array()){
    $where='';
    if(!empty($condition)){
      foreach($condition as $k=>$v){
        $where.=$k."='".$v."' and ";
      }
      $where='where '.$where .'1=1';
    }
    $fieldstr = '';
    if(!empty($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 = '';
    $data = '';
    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(!empty($condition)) {
    foreach($condition as $k=>$v) {
      $where.=$k."='".$v."' and ";
    }
      $where='where '.$where .'1=1';
    }
    $updatastr = '';
    if(!empty($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(!empty($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>";
?>

更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。