include文件------------------------------
-------------------------init.php
------------------------------------config.class.php
--------------------------------------------------------config.inc.php
------------------------------------mysql.class.php
--------------------------------------------------------db.class.php
-------------------------------------log.class.php
-------------------------------------lib_base.php
<?php /**** db.class.php ****/ /*** file db.class.php 数据库类
目前到底采用什么数据库 还不清楚 ***/ abstract class db {
/* 连接服务器 parms $h 服务器地址 parms $u 用户名 parms $p 密码 return bool */ public abstract function connect($h,$u,$p);
/* 发送查询 parms $sql 发送的sql语句 return mixed bool/resource */ public abstract function query($sql);
/* 查询多行数据 parms $sql select型语句 return array/bool */ public abstract function getAll($sql);
/* 查询单行数据 parms $sql select型语句 return array/bool */ public abstract function getRow($sql);
/* 查询单个数据 parms $sql select型语句 return array/bool */ public abstract function getOne($sql);
/* 自动执行insert/update语句 parms $sql select型语句 return array/bool
$this->autoExecute(‘user‘,array(‘username‘=>‘zhansan‘,‘email‘=>‘zhang@163.com‘),‘insert‘); 将发生 自动形成 insert into user (username,email) values (‘zhangsan‘,‘zhang@163.com‘);
*/ public abstract function autoExecute($table,$data,$act=‘insert‘,$where=‘‘);
} |
<?php /**** mysql.class.php ****/ class mysql extends db { private static $ins = NULL; private $conn = NULL; private $conf = array();
protected function __construct() { $this->conf = conf::getIns();
$this->connect($this->conf->host,$this->conf->user,$this->conf->pwd); $this->select_db($this->conf->db); $this->setChar($this->conf->char); }
public function __destruct() { }
public static function getIns() { if(!(self::$ins instanceof self)) { self::$ins = new self(); }
return self::$ins; }
public function connect($h,$u,$p) { $this->conn = mysql_connect($h,$u,$p); if(!$this->conn) { $err = new Exception(‘连接失败‘); throw $err; } }
protected function select_db($db) { $sql = ‘use ‘ . $db; $this->query($sql); }
protected function setChar($char) { $sql = ‘set names ‘ . $char; return $this->query($sql); }
public function query($sql) {
$rs = mysql_query($sql,$this->conn);
log::write($sql);
return $rs; }
public function autoExecute($table,$arr,$mode=‘insert‘,$where = ‘ where 1 limit 1‘) { /* insert into tbname (username,passwd,email) values (‘‘,) /// 把所有的键名用‘,‘接起来 // implode(‘,‘,array_keys($arr)); // implode("‘,‘",array_values($arr)); */
if(!is_array($arr)) { return false; }
if($mode == ‘update‘) { $sql = ‘update ‘ . $table .‘ set ‘; foreach($arr as $k=>$v) { $sql .= $k . "=‘" . $v ."‘,"; } $sql = rtrim($sql,‘,‘); $sql .= $where;
return $this->query($sql); }
$sql = ‘insert into ‘ . $table . ‘ (‘ . implode(‘,‘,array_keys($arr)) . ‘)‘; $sql .= ‘ values (\‘‘; $sql .= implode("‘,‘",array_values($arr)); $sql .= ‘\‘)‘;
return $this->query($sql);
}
public function getAll($sql) { $rs = $this->query($sql);
$list = array(); while($row = mysql_fetch_assoc($rs)) { $list[] = $row; }
return $list; }
public function getRow($sql) { $rs = $this->query($sql);
return mysql_fetch_assoc($rs); }
public function getOne($sql) { $rs = $this->query($sql); $row = mysql_fetch_row($rs);
return $row[0]; }
// 返回影响行数的函数 public function affected_rows() { return mysql_affected_rows($this->conn); }
// 返回最新的auto_increment列的自增长的值 public function insert_id() { return mysql_insert_id($this->conn); } }
|
测试mysql类
在init.php中引入文件
define(‘ROOT‘, str_replace(‘\\‘, ‘/‘, dirname(dirname(__FILE__))).‘/‘); require(ROOT."Model/Model.class.php"); require(ROOT."Model/testModel.class.php"); require(ROOT."include/config.inc.php"); require(ROOT."include/config.class.php"); require(ROOT."include/log.class.php"); require(ROOT."include/db.class.php"); require(ROOT."include/mysql.class.php"); |
在config.inc.php中配置
$_CFG = array(); $_CFG["host"] = "localhost"; $_CFG["user"] = "root"; $_CFG["pwd"] = ""; $_CFG["db"] = "boolshop"; $_CFG["char"] = "utf8"; |
开始测试
//单例模式获取实例 $mysql = mysql::getIns(); var_dump($mysql); |
输出结果为:
object(mysql)#1 (2) { ["conn:private"]=> resource(8) of type (mysql link) ["conf:private"]=> object(conf)#2 (1) { ["data:protected"]=> array(5) { ["host"]=> string(9) "localhost" ["user"]=> string(4) "root" ["pwd"]=> string(0) "" ["db"]=> string(8) "boolshop" ["char"]=> string(4) "utf8" } } }
|
继续测试:
$mysql = mysql::getIns(); $t1 = $_GET["t1"]; $t2 = $_GET["t2"]; $sql = "insert into test(t1,t2) values(‘$t1‘,‘$t2‘)"; var_dump($mysql->query($sql)); var_dump($mysql->autoExcute(‘test‘,$_GET,‘insert‘)); |
本文出自 “杜国栋个人PHP学习博文” 博客,请务必保留此出处http://duguodong.blog.51cto.com/7667978/1387195