>  기사  >  백엔드 개발  >  PHP 싱글톤 모드의 간단한 사용량 공유

PHP 싱글톤 모드의 간단한 사용량 공유

小云云
小云云원래의
2018-02-12 10:02:021293검색

이 글은 주로 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

php

php에서 싱글턴 모드를 구현하는 방법

🎜🎜PHP 디자인 모드에서 팩토리 모드와 싱글턴 모드의 차이점🎜 🎜

위 내용은 PHP 싱글톤 모드의 간단한 사용량 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.