养浩然之气,活着不是为技术,请关心身边的人.
Database object
上一篇 / 下一篇 2007-11-02 17:26:10 / 个人分类:php
class Dbstatistic{
private static $databaseinfo;
function __construct(){
$this->databaseinfo['host']="localhost";
$this->databaseinfo['user']="root";
$this->databaseinfo['password']="111111";
}
function get_database_info(){
return $this->databaseinfo;
}
}
class Db{
private static $instance;
private $host,$user,$password; // 主机,用户,密码
private $database; // 数据库名
private $linkid; // mysql 连接标识
private $result; // mysql resource
private $select_result; //result collection
private $record_set = array(); // 当前记录集
private $eof_record_set; // 结果集 结尾标志
public $num_rows = 0; // 记录数
public $affected_rows = 0; // 受影响的记录数
public $insert_id; // insert 操作的id
public $current_row_num = 0; // 当前记录行号
public $debug = false; // 排错开关
public $sql_count = 0; // sql 语句计数
public $sql_statu = array(); // sql语句资料
/**
* @return void
* @param string $host
* @param string $user
* @param string $password
* @desc
* Db 类构造函数
* $host : 数据库读务器名
* $user : 用户名
* $password : 密码
*/
function __construct($databaseinfo){
if (empty($databaseinfo['host']) || empty($databaseinfo['user']) || !isset($databaseinfo['password'])){
trigger_error("无法实例化 Db 类", E_USER_ERROR);
}
$this->host = $databaseinfo['host'];
$this->user = $databaseinfo['user'];
$this->password = $databaseinfo['password'];
}
/**
* @return void
* @param string $database
* @desc
* 连接数据库服务器并选择数据库
* $database : 数据库名
*/
public function connect($database){
if ( null == ($this->linkid = @mysql_connect($this->host, $this->user, $this->password))){
trigger_error("无法连接数据库服务器 $this->host", E_USER_ERROR);
}else {
if ( false == mysql_select_db($database,$this->linkid) ){
trigger_error("$database 不存在于服务器 $this->host", E_USER_ERROR);
}
}
$this->database = $database;
}
/**
* @return void
* @param string $sql
* @desc
* 执行 SQL 查询,获得 结果集和查询影响的记录数
* 在发生 SELECT 查询时 标志当前记录行为0,标志记录集开始(0)
* 在发生 INSERT 查询时 获得 insert 查询后的id值
* $sql : SQL 语句
*/
public function query($sql){
$this->sql_statu[$this->sql_count]["sql"] = $sql; // 记录 sql 语句数据
if ( false == ($this->result = mysql_query($sql, $this->linkid)) ){
$this->sql_statu[$this->sql_count]["run"] = "error";
trigger_error("SQL 语句错误<br>$sql", E_USER_ERROR);
}else {
$sql_command = strtoupper(substr(trim($sql), 0,6));
switch ($sql_command){ // 获取 查询所得的记录数目
case "SELECT":
$this->get_num_rows();
$this->current_row_num = 0; // 当前行号 标志 为 0
$this->eof_record_set = 0; // 结果集 开始
break;
case "INSERT":
$this->get_affected_rows();
$this->get_insert_id();
return $this->insert_id;
break;
case "UPDATE":
$this->get_affected_rows();
break;
case "DELETE":
$this->get_affected_rows();
break;
}
$this->sql_statu[$this->sql_count]["run"] = "ok";
$this->sql_statu[$this->sql_count]["num_rows"] = $this->num_rows;
$this->sql_statu[$this->sql_count]["affected_rows"] = $this->affected_rows;
$this->sql_statu[$this->sql_count]["insert_id"] = $this->insert_id;
$this->sql_count++; // sql 语句 计数
}
}
/**
* @return bool
* @desc
* 获得当前记录行数据,并把记录集指针移到下一条记录
* 操作成功 返回 true, 失败 返回 false;
*/
public function next_record(){
if (false == ($this->record_set = mysql_fetch_array($this->result)) ){
// $this->current_row_num = $this->num_rows - 1;
$this->eof_record_set = 1; // 结果集 结束
return false;
}else {
$this->current_row_num++;
return true;
}
}
/**
* @return void
* @param int $row_number
* @desc
* 把记录集指针移到指定的记录行
* $row_number : 记录集行号
*/
public function record_seek($row_number){
if ($row_number < 0 || $row_number >= $this->num_rows){
trigger_error("错误的记录行号", E_USER_ERROR);
}else {
if (false == mysql_data_seek($this->result, $row_number)){
trigger_error("跳转至指定的记录行失败", E_USER_ERROR);
}else{
$this->current_row_num = $row_number;
}
}
}
/**
* @return mix
* @param string $fields_name
* @desc
* 返回当前记录行中相同字段名的值
* 当达到记录集末尾时 返回 false
* $fields_name : 字段名
*/
public function f($fields_name){
if (1 == $this->eof_record_set) {
return false;
}
if (false == isset($this->record_set[$fields_name])){
trigger_error("无效的字段名", E_USER_ERROR);
}else {
return $this->record_set[$fields_name];
}
}
/**
* @return void
* @desc
* 获得当前记录集中的记录数
*/
private function get_num_rows(){
$this->num_rows = mysql_num_rows($this->result);
}
/**
* @return void
* @desc
* 获得被 sql 语句影响的记录数
*/
private function get_affected_rows(){
$this->affected_rows = mysql_affected_rows($this->linkid);
}
/**
* @return void
* @desc
* 获得 insert 语句操作后的 id
*/
public function get_insert_id(){
$this->insert_id = mysql_insert_id($this->linkid);
}
private function output_debug(){
print '<br />------------------------------------------<br />';
print "页面使用的 sql 语句有 $this->sql_count 条:<br />";
print '<pre>';
print_r($this->sql_statu);
print '</pre>';
}
/**
* @return mix
* @param string $sql,$fetch(optional)
* @desc
* 执行 SQL 查询,获得 结果集
* $sql : SQL 语句
*/
function get_result($sql,$fetch="default"){
$this->query($sql);
if(strtolower(trim($fetch))=="default" || strtolower(trim($fetch))=="assoc"){
while($row=mysql_fetch_assoc($this->result))
{
$this->select_result[]=$row;
}
}
else if(strtolower(trim($fetch))=="array")
{
while($row=mysql_fetch_array($this->result))
{
$this->select_result[]=$row;
}
}
else if(strtolower(trim($fetch))=="object")
{
while($row=mysql_fetch_object($this->result))
{
$this->select_result[]=$row;
}
}
else if(strtolower(trim($fetch))=="row")
{
while($row=mysql_fetch_row($this->result))
{
$this->select_result[]=$row;
}
}
return $this->select_result;
}
}
//the container
class Dbcontainer{
private $instance;
private $database;
function __construct()
{
$this->database=new Dbstatistic();
}
function register_instance($database)
{
if(!empty($this->instance) && array_key_exists($database,$this->instance)){}
else
{
$Dbinstance=new Db($this->database->get_database_info());
$Dbinstance->connect($database);
$this->instance[$database]=$Dbinstance;
}
}
function get_instance($database)
{
if(!empty($this->instance) && array_key_exists($database,$this->instance))
{
return $this->instance[$database];
}
else
trigger_error("Instance $database not register", E_USER_ERROR);
}
}
$Dbcontainer=new Dbcontainer();
$query = "select * from category";
$Dbcontainer->register_instance("exam");
$Dbinstance1=$Dbcontainer->get_instance("exam");
$result=$Dbinstance1->get_result($query,"object");
var_dump($result);
echo "=========================================================";
$Dbcontainer->register_instance("gallery");
$Dbinstance2=$Dbcontainer->get_instance("gallery");
$result=$Dbinstance2->get_result($query,"object");
var_dump($result);
private static $databaseinfo;
function __construct(){
$this->databaseinfo['host']="localhost";
$this->databaseinfo['user']="root";
$this->databaseinfo['password']="111111";
}
function get_database_info(){
return $this->databaseinfo;
}
}
class Db{
private static $instance;
private $host,$user,$password; // 主机,用户,密码
private $database; // 数据库名
private $linkid; // mysql 连接标识
private $result; // mysql resource
private $select_result; //result collection
private $record_set = array(); // 当前记录集
private $eof_record_set; // 结果集 结尾标志
public $num_rows = 0; // 记录数
public $affected_rows = 0; // 受影响的记录数
public $insert_id; // insert 操作的id
public $current_row_num = 0; // 当前记录行号
public $debug = false; // 排错开关
public $sql_count = 0; // sql 语句计数
public $sql_statu = array(); // sql语句资料
/**
* @return void
* @param string $host
* @param string $user
* @param string $password
* @desc
* Db 类构造函数
* $host : 数据库读务器名
* $user : 用户名
* $password : 密码
*/
function __construct($databaseinfo){
if (empty($databaseinfo['host']) || empty($databaseinfo['user']) || !isset($databaseinfo['password'])){
trigger_error("无法实例化 Db 类", E_USER_ERROR);
}
$this->host = $databaseinfo['host'];
$this->user = $databaseinfo['user'];
$this->password = $databaseinfo['password'];
}
/**
* @return void
* @param string $database
* @desc
* 连接数据库服务器并选择数据库
* $database : 数据库名
*/
public function connect($database){
if ( null == ($this->linkid = @mysql_connect($this->host, $this->user, $this->password))){
trigger_error("无法连接数据库服务器 $this->host", E_USER_ERROR);
}else {
if ( false == mysql_select_db($database,$this->linkid) ){
trigger_error("$database 不存在于服务器 $this->host", E_USER_ERROR);
}
}
$this->database = $database;
}
/**
* @return void
* @param string $sql
* @desc
* 执行 SQL 查询,获得 结果集和查询影响的记录数
* 在发生 SELECT 查询时 标志当前记录行为0,标志记录集开始(0)
* 在发生 INSERT 查询时 获得 insert 查询后的id值
* $sql : SQL 语句
*/
public function query($sql){
$this->sql_statu[$this->sql_count]["sql"] = $sql; // 记录 sql 语句数据
if ( false == ($this->result = mysql_query($sql, $this->linkid)) ){
$this->sql_statu[$this->sql_count]["run"] = "error";
trigger_error("SQL 语句错误<br>$sql", E_USER_ERROR);
}else {
$sql_command = strtoupper(substr(trim($sql), 0,6));
switch ($sql_command){ // 获取 查询所得的记录数目
case "SELECT":
$this->get_num_rows();
$this->current_row_num = 0; // 当前行号 标志 为 0
$this->eof_record_set = 0; // 结果集 开始
break;
case "INSERT":
$this->get_affected_rows();
$this->get_insert_id();
return $this->insert_id;
break;
case "UPDATE":
$this->get_affected_rows();
break;
case "DELETE":
$this->get_affected_rows();
break;
}
$this->sql_statu[$this->sql_count]["run"] = "ok";
$this->sql_statu[$this->sql_count]["num_rows"] = $this->num_rows;
$this->sql_statu[$this->sql_count]["affected_rows"] = $this->affected_rows;
$this->sql_statu[$this->sql_count]["insert_id"] = $this->insert_id;
$this->sql_count++; // sql 语句 计数
}
}
/**
* @return bool
* @desc
* 获得当前记录行数据,并把记录集指针移到下一条记录
* 操作成功 返回 true, 失败 返回 false;
*/
public function next_record(){
if (false == ($this->record_set = mysql_fetch_array($this->result)) ){
// $this->current_row_num = $this->num_rows - 1;
$this->eof_record_set = 1; // 结果集 结束
return false;
}else {
$this->current_row_num++;
return true;
}
}
/**
* @return void
* @param int $row_number
* @desc
* 把记录集指针移到指定的记录行
* $row_number : 记录集行号
*/
public function record_seek($row_number){
if ($row_number < 0 || $row_number >= $this->num_rows){
trigger_error("错误的记录行号", E_USER_ERROR);
}else {
if (false == mysql_data_seek($this->result, $row_number)){
trigger_error("跳转至指定的记录行失败", E_USER_ERROR);
}else{
$this->current_row_num = $row_number;
}
}
}
/**
* @return mix
* @param string $fields_name
* @desc
* 返回当前记录行中相同字段名的值
* 当达到记录集末尾时 返回 false
* $fields_name : 字段名
*/
public function f($fields_name){
if (1 == $this->eof_record_set) {
return false;
}
if (false == isset($this->record_set[$fields_name])){
trigger_error("无效的字段名", E_USER_ERROR);
}else {
return $this->record_set[$fields_name];
}
}
/**
* @return void
* @desc
* 获得当前记录集中的记录数
*/
private function get_num_rows(){
$this->num_rows = mysql_num_rows($this->result);
}
/**
* @return void
* @desc
* 获得被 sql 语句影响的记录数
*/
private function get_affected_rows(){
$this->affected_rows = mysql_affected_rows($this->linkid);
}
/**
* @return void
* @desc
* 获得 insert 语句操作后的 id
*/
public function get_insert_id(){
$this->insert_id = mysql_insert_id($this->linkid);
}
private function output_debug(){
print '<br />------------------------------------------<br />';
print "页面使用的 sql 语句有 $this->sql_count 条:<br />";
print '<pre>';
print_r($this->sql_statu);
print '</pre>';
}
/**
* @return mix
* @param string $sql,$fetch(optional)
* @desc
* 执行 SQL 查询,获得 结果集
* $sql : SQL 语句
*/
function get_result($sql,$fetch="default"){
$this->query($sql);
if(strtolower(trim($fetch))=="default" || strtolower(trim($fetch))=="assoc"){
while($row=mysql_fetch_assoc($this->result))
{
$this->select_result[]=$row;
}
}
else if(strtolower(trim($fetch))=="array")
{
while($row=mysql_fetch_array($this->result))
{
$this->select_result[]=$row;
}
}
else if(strtolower(trim($fetch))=="object")
{
while($row=mysql_fetch_object($this->result))
{
$this->select_result[]=$row;
}
}
else if(strtolower(trim($fetch))=="row")
{
while($row=mysql_fetch_row($this->result))
{
$this->select_result[]=$row;
}
}
return $this->select_result;
}
}
//the container
class Dbcontainer{
private $instance;
private $database;
function __construct()
{
$this->database=new Dbstatistic();
}
function register_instance($database)
{
if(!empty($this->instance) && array_key_exists($database,$this->instance)){}
else
{
$Dbinstance=new Db($this->database->get_database_info());
$Dbinstance->connect($database);
$this->instance[$database]=$Dbinstance;
}
}
function get_instance($database)
{
if(!empty($this->instance) && array_key_exists($database,$this->instance))
{
return $this->instance[$database];
}
else
trigger_error("Instance $database not register", E_USER_ERROR);
}
}
$Dbcontainer=new Dbcontainer();
$query = "select * from category";
$Dbcontainer->register_instance("exam");
$Dbinstance1=$Dbcontainer->get_instance("exam");
$result=$Dbinstance1->get_result($query,"object");
var_dump($result);
echo "=========================================================";
$Dbcontainer->register_instance("gallery");
$Dbinstance2=$Dbcontainer->get_instance("gallery");
$result=$Dbinstance2->get_result($query,"object");
var_dump($result);
相关阅读:
- 几大网站使用的操作系统、Web服务器和开发语言 (wanggen0704, 2007-10-31)
- php技术blog开通了 (来影, 2007-10-31)
- IOC详解 (xiaotian_ls, 2007-11-01)
- php中ioc (xiaotian_ls, 2007-11-01)
- 变量函数 (xiaotian_ls, 2007-11-01)
- PHP编程“数组”的基础知识 (荆柯要上网, 2007-11-01)
- ZendFramework分目录结构的类名写法 (来影, 2007-11-01)
- PHP~ (simaopig, 2007-11-02)
- 25个顶级PHP模板引擎 (phperwuhan, 2007-11-02)
- [转] PHP File Upload (dennis, 2007-11-02)
TAG: php
