PHP单例模式使用方法详解

墨辰丷
发布: 2023-03-27 07:40:02
原创
1227 人浏览过

这篇文章主要介绍了PHP单例模式简单用法,结合数据库操作类实例形式分析了php单例模式的具体实现方法与使用技巧,需要的朋友可以参考下

具体如下:

<?php
class db {
  public $conn;
  public static $sql;
  public static $instance=null;
  private function __construct(){
    require_once(&#39;db.config.php&#39;);
    $this->conn = mysql_connect($db[&#39;host&#39;],$db[&#39;user&#39;],$db[&#39;password&#39;]);
    if(!mysql_select_db($db[&#39;database&#39;],$this->conn)){
      echo "失败";
    };
    mysql_query(&#39;set names utf8&#39;,$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=&#39;&#39;;
    if(!empty($condition)){
      foreach($condition as $k=>$v){
        $where.=$k."=&#39;".$v."&#39; and ";
      }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    $fieldstr = &#39;&#39;;
    if(!empty($field)){
      foreach($field as $k=>$v){
        $fieldstr.= $v.&#39;,&#39;;
      }
      $fieldstr = rtrim($fieldstr,&#39;,&#39;);
    } else {
      $fieldstr = &#39;*&#39;;
    }
    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 = &#39;&#39;;
    $data = &#39;&#39;;
    foreach ($data as $k=>$v) {
      $values .= $k.&#39;,&#39;;
      $datas .= "&#39;$v&#39;".&#39;,&#39;;
    }
    $values = rtrim($values,&#39;,&#39;);
    $datas = rtrim($datas,&#39;,&#39;);
    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=&#39;&#39;;
    if(!empty($condition)) {
    foreach($condition as $k=>$v) {
      $where.=$k."=&#39;".$v."&#39; and ";
    }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    $updatastr = &#39;&#39;;
    if(!empty($data)) {
    foreach($data as $k=>$v) {
      $updatastr.= $k."=&#39;".$v."&#39;,";
    }
      $updatastr = &#39;set &#39;.rtrim($updatastr,&#39;,&#39;);
    }
    self::$sql = "update {$table} {$updatastr} {$where}";
    return mysql_query(self::$sql);
  }
  //删除记录
  public function delete($table,$condition) {
    $where=&#39;&#39;;
    if(!empty($condition)) {
      foreach($condition as $k=>$v) {
        $where.=$k."=&#39;".$v."&#39; and ";
      }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    self::$sql = "delete from {$table} {$where}";
    return mysql_query(self::$sql);
  }
  public static function getLastSql() {
    echo self::$sql;
  }
}
$db = db::getInstance();
//$list = $db->select(&#39;demo&#39;,array(&#39;name&#39;=>&#39;tom&#39;,&#39;password&#39;=>&#39;ds&#39;),array(&#39;name&#39;,&#39;password&#39;));
//echo $db->insert(&#39;demo&#39;,array(&#39;name&#39;=>&#39;最近你啦&#39;,&#39;password&#39;=>&#39;123&#39;));
//echo $db->update(&#39;demo&#39;,array("name"=>&#39;xxx&#39;,"password"=>&#39;123&#39;),array(&#39;id&#39;=>1));
echo $db->delete(&#39;demo&#39;,array(&#39;id&#39;=>&#39;2&#39;));
db::getLastSql();
echo "<pre class="brush:php;toolbar:false">";
?>
登录后复制

相关推荐:

php实现mongoDB单例模式操作类步骤详解

PHP中单例模式与工厂模式详解及实例

PHP单例模式使用案例详解

以上是PHP单例模式使用方法详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板