?php
//包含数据库操作类文件
include 'mysql.class.php';
//设置传入参数
$hostname='localhost';
$username='root';
$password='123456';
$dbname='aisi';
$charset = 'utf8';
//实例化对象
$db = new Mysql($hostname,$username,$password,$dbname);
//获取一条数据
$sql = "SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1";
$count = $db-getOne($sql);
//获取多条数据
$sql = "SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";
$service = $db-getAll($sql);
//插入数据
$arr = array(
'as_article_title'='数据库操作类',
'as_article_author'='rex',
);
$res = $db-insert('as_article',$arr);
//更新数据
$arr = array(
'as_article_title'='实例化对象',
'as_article_author'='Lee',
);
$where = "as_article_id=1";
$res = $db-update('as_article',$arr,$where);
//删除数据
$where = "as_article_id=1";
$res = $db-del('as_article',$where);
?
php封装一个class类实现mysql数据库的增删该查?php
class db{
private $db;
const MYSQL_OPT_READ_TIMEOUT = 11;
const MYSQL_OPT_WRITE_TIMEOUT = 12;
private $tbl_name;
private $where;
private $sort;
private $fields;
private $limit;
public static $_instance = null;
function __construct(){
$cfg = loadConfig('db');
$db = mysqli_init();
$db-options(self::MYSQL_OPT_READ_TIMEOUT, 3);
$db-options(self::MYSQL_OPT_WRITE_TIMEOUT, 1);
@$db-real_connect($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);
if ($db-connect_error) {
$this-crash($db-errno,$db-error);
}
$db-set_charset("utf8");
$this-db = $db;
//echo $this-db-stat;
}
public static function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self();
}
return self::$_instance;
}
private function __clone() {}//覆盖__clone()方法php连接数据库类封装,禁止克隆
public function find($conditions = null){
if($conditions) $this-where($conditions);
return $this-getArray($this-buildSql(),1);
}
public function findAll($conditions = null){
if($conditions) $this-where($conditions);
return $this-getArray($this-buildSql());
}
//表
public function t($table){ $this-tbl_name = $table; return $this;}
//条件
public function where($conditions){
$where = '';
if(is_array($conditions)){
$join = array();
foreach( $conditions as $key = $condition ){
$condition = $this-db-real_escape_string($condition);
$join[] = "`{$key}` = '{$condition}'";
}
$where = "WHERE ".join(" AND ",$join);
}else{
if(null != $conditions) $where = "WHERE ".$conditions;
}
$this-where = $where;
return $this;
}
//排序
public function sort($sort){
if(null != $sort) $sort = "ORDER BY {$sort}";
$this-sort = $sort;
return $this;
}
//字段
public function fields($fields){$this-fields = $fields; return $this; }
public function limit($limit){$this-limit = $limit; return $this;}
private function buildSql(){
$this-fields = empty($this-fields) ? "*" : $this-fields;
$sql = "SELECT {$this-fields} FROM {$this-tbl_name} {$this-where} {$this-sort}";
accessLog('db_access',$sql);
if(null != $this-limit)$sql .= " limit {$this-limit}";
return $sql;
}
/**
* 返回查询数据
* @param$sql
* @param bool $hasOne
* @return array|bool|mixed
*/
private function getArray($sql,$hasOne = false){
if($this-db-real_query($sql) ){
if ($result = $this-db-use_result()) {
推荐阅读
- 用网页看虎牙直播很卡吗,为什么浏览器看虎牙直播很卡
- 数据结构与算法c语言版徐凤生,数据结构与算法教程,数据结构c语言版教程!
- javaredis实现商品秒杀,redis抢购java
- ios如何播放优酷视频格式,ios如何播放优酷视频格式文件
- vb.net窗体透明设置 vb窗体不透明度
- postgresql字段拼接,pgsql 拼接
- 模拟经营外星人医院游戏,模拟经营外星人医院游戏怎么玩
- 微信视频号被注销,微信视频号被注销了怎么注册
- 代码java开发 java 代码生成器 开源