php封装的数据库 php封装数据库类mvc( 三 )


function
fetch_assoc($result)
{
return
mysql_fetch_assoc($result);
}
//从结果集提取当前行 , 以字段名和数字为key表示的关联数组形式返回
function
fetch_array($result)
{
return
mysql_fetch_array($result);
}
//关闭链接
function
close()
{
return
mysql_close($this-dblink)
;
}
//输出简单的错误html提示信息并终止程序
function
halt($msg)
{
$message
=
"html\nhead\n"
;
$message
.=
"meta
content='text/html;charset=gb2312'\n"
;
$message
.=
"/head\n"
;
$message
.=
"body\n"
;
$message
.=
"数据库出错:".htmlspecialchars($msg)."\n"
;
$message
.=
"/body\n"
;
$message
.=
"/html"
;
echo
$message
;
exit
;
}
}
?
php封装一个class类,实现mysql数据库的增删改查怎么操做?class sqlHelper{\x0d\x0apublic $conn;\x0d\x0apublic $dbname="数据库名称";\x0d\x0apublic $username="数据库用户名";\x0d\x0apublic $password="数据库密码";\x0d\x0apublic $host="localhost";\x0d\x0a//连接数据库\x0d\x0apublic function __construct(){\x0d\x0a$this-conn=mysql_connect($this-host,$this-username,$this-password);\x0d\x0aif(!$this-conn){\x0d\x0adie("连接失败".mysql_error());\x0d\x0a}\x0d\x0amysql_select_db($this-dbname,$this-conn);\x0d\x0a}\x0d\x0a//执行查询语句\x0d\x0apublic function execute_dql($sql){\x0d\x0a$res=mysql_query($sql,$this-conn);\x0d\x0areturn $res;\x0d\x0a}\x0d\x0a//执行增填改语句\x0d\x0apublic function execute_dml($sql){\x0d\x0a$b=mysql_query($sql,$this-conn);\x0d\x0aif(!$b){\x0d\x0areturn 3;\x0d\x0a}else{\x0d\x0aif(mysql_affected_rows($this-conn)){\x0d\x0areturn 1;//表示OK\x0d\x0a}else{\x0d\x0areturn 2;//表示没有行收到影响\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}
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;

推荐阅读