PHP单例模式简单用法示例

前端之家收集整理的这篇文章主要介绍了PHP单例模式简单用法示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP单例模式用法分享给大家供大家参考,具体如下:

PHP;"> 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 "
";
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

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

原文链接:https://www.f2er.com/php/17236.html

猜你在找的PHP相关文章