mysql 单例模式

require_once ('mysql_config.php');
	class msyql_db{ 
	  static private $_instance;
	  private $com;
	  private function __construct($db_host,$name,$db_name,$db_pwd){   //外部不能直接实例化
	   $this->com=mysql_connect($db_host,$name,$db_pwd) or die("链接数据库失败");
	   mysql_select_db($db_name);
	   mysql_query("set names utf8");
	  }
	
	  private function __clone(){}   //外部不能克隆类
	
	  static public function instance_com($dbhost,$name,$dbname,$sbpwd){
	   if(!isset(self::$_instance)){    //self表示类本身  调用静态属性时要使用 ::
	    self::$_instance= new self($dbhost,$name,$dbname,$sbpwd);  //实例化自己
	    return self::$_instance;
	   }else{
	
	     return self::$_instance;
	
	   }
	  }
	
	  function select($sql){ //获得多条数据,二维数组
	   	$result=mysql_query($sql);
	    $arr=array();
	    while($row=mysql_fetch_assoc($result)){
	    	$arr[]=$row;
	    }
	    return $arr;
	  }
	  
	  function select1($sql){
	  	$result=mysql_query($sql);
	  	return mysql_fetch_assoc($result);
	  }
	  
	  function count($sql){
	  		$result=mysql_query($sql);
	  		return mysql_num_rows($result);
	  }
	  
	  function query($sql){
	  	return mysql_query($sql);
	  }
	  
	  function get_last_id(){
	  	$sql="select last_insert_id() as last_id";
	  	$result=mysql_query($sql);
	  	$result=mysql_fetch_assoc($result);
	  	return $result["last_id"];
	  }
	  
	  function insert($post){  //insert 语句 合并字段和对应的值
  		$cloum="";
		$value="";
		foreach ($post as $k=>$v){
			if ($k!="type"){
				$cloum .= ",`".$k."`";
				$value .= ",'".$v."'";
			}
		}
		$cloum=substr($cloum,1);
		$value=substr($value,1);
		return array("cloum"=>$cloum,"value"=>$value);
	  }
	  
	  function update($post){ //update 语句 合并字段和对应的值
	  	$str="";
	  	foreach ($post as $k=>$v){
	  		if ($k!="type"){
	  			$str .= ",`".$k."`='".$v."'";
	  		}
	  	}
	  	return substr($str,1);
	  }
	  
	 }
	
	 //$test=new one("localhost","root","online","");
	
	 $db = msyql_db::instance_com($db_host,$db_user,$db_db,$db_pwd);  //调用静态方法的时候要使用::

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。