<?php
class MySQL
{
var $serverName = ''; //数据库主机
var $admin ='';
var $password ='';
var $dbName = ''; //数据库名
var $dbUsername = ''; // 数据库用户名
var $dbPassword = ''; // 数据库密码
var $usePconnect = 0;
var $website ='';
var $id = 0;
var $linkId = 0;
var $queryId = 0;
var $queryCount = 0;
var $result;
var $record = array();
var $rows;
var $affectedRows = 0;
var $insertId;
var $errno;
var $error;
var $queryLog = array();
function GetErrDesc()
{
$this->error = @mysql_error( $this->linkId );
return $this->error;
}
function GetErrNo()
{
$this->errno = @mysql_errno( $this->linkId );
return $this->errno;
}
function Connect()
{
if ( $this->usePconnect == 1 )
{
if ( !$this->linkId = @mysql_pconnect( $this->serverName, $this->dbUsername, $this->dbPassword ) )
$this->Halt( 'Connect faild!' );
}
else
{
if ( !$this->linkId = @mysql_connect( $this->serverName, $this->dbUsername, $this->dbPassword ) )
$this->Halt( 'Connect faild!' );
}
return $this->linkId;
}
function SelectDB()
{
if ( !mysql_select_db( $this->dbName ) )
$this->Halt( 'Connect faild!' );
}
function Query( $queryStr )
{
$this->result = mysql_query( $queryStr, $this->linkId );
if ( !$this->result )
$this->Halt( 'Invalid SQL: ' . $queryStr );
return $this->result;
}
function Update( $queryStr )
{
$this->Query( $queryStr );
return $this->AffectedRows();
}
function FetchArray( $queryId, $fetchType = 'assoc' )
{
if ( empty( $queryId ) )
$this->Halt( 'Invalid query id:' . $queryId );
if ( $fetchType = 'assoc' )
$this->record = mysql_fetch_assoc( $queryId );
else
$this->record = mysql_fetch_array( $queryId );
return $this->record;
}
function FetchRow( $queryId )
{
if ( empty( $queryId ) )
$this->Halt( 'Invalid query id:' . $queryId );
$this->record = mysql_fetch_row( $queryId );
return $this->record;
}
function FetchOne( $query, $field = '' )
{
if ( empty( $query ) )
$this->Halt( 'Invalid query id:' . $query );
$this->result = $this->Query( $query );
$this->record = $this->FetchArray( $this->result );
if ( $field != '' )
return $this->record[$field];
else
return $this->record;
}
function FetchAll( $query, $field = '' )
{
if ( empty( $query ) )
$this->Halt( 'Invalid query id:' . $query );
$this->result = $this->Query( $query );
if ( $field != '' )
{
while ( $this->record = $this->FetchArray( $this->result ) )
$result[] = $this->record[$field];
}
else
{
while ( $this->record = $this->FetchArray( $this->result ) )
$result[] = $this->record;
}
return $result;
}
function NumRows( $queryId )
{
if ( empty( $queryId ) )
$this->Halt( 'Invalid query id:' . $queryId );
$this->rows = mysql_num_rows( $queryId );
return $this->rows;
}
function AffectedRows()
{
$this->AffectedRows = mysql_affected_rows( $this->linkId );
return $this->AffectedRows;
}
function FreeResult( $query )
{
if ( !mysql_free_result( $query ) )
$this->Halt( 'Fail to mysql_free_result' );
}
function InsertId()
{
$this->insertId = mysql_insert_id();
if ( !$this->insertIid )
$this->Halt( 'Fail to get mysql_insert_id' );
return $this->insertId;
}
function Close()
{
@mysql_close( $this->linkId );
}
function Halt( $msg )
{
$message = "<html>\n<head>\n";
$message .= "<meta content=\"text/html; charset=GBK\" http-equiv=\"Content-Type\">\n";
$message .= "<style type=\"text/css\">\n";
$message .= "body,td,p,pre {font-family : Verdana, Arial;font-size : 14px;}\n";
$message .= "</style>\n";
$message .= "</head>\n";
$message .= "<body>\n";
$content = '<p>MySQL Database Error!!!</p><pre><b>' . htmlspecialchars( $msg ) . "</b></pre>\n";
$content .= '<b>MySQL error description</b>: ' . $this->GetErrDesc() . "\n<br>";
$content .= '<b>MySQL error number</b>: ' . $this->GetErrNo() . "\n<br>";
$content .= '<b>Date</b>: ' . date( 'Y-m-d @ H:i' ) . "\n<br>";
$content .= '<b>Script</b>: http://' . $_SERVER['HTTP_HOST'] . getenv( 'REQUEST_URI' ) . "\n<br><br>";
$message .= $content;
$message .= "</body>\n</html>";
echo $message;
exit;
}
function NR( $queryId )
{
return $this->NumRows( $queryId );
}
function FM( $sql, $field = '' )
{
return $this->FetchAll( $sql, $field );
}
function FA( $queryId, $fetchType = 'assoc' )
{
return $this->FetchArray( $queryId, $fetchType );
}
function FO( $query, $field = '' )
{
return $this->FetchOne( $query, $field );
}
function QY( $queryStr )
{
return $this->Query( $queryStr );
}
function AR()
{
return $this->AffectedRows();
}
}
?>
积分 40 阅读权限 10 在线时间 28 小时 注册时间 2008-3-28 最后登录 2010-7-1 查看详细资料
TOP
深空
可爱宝贝
荣誉管理团队
认证 帖子 9874 体力 12299 威望 57 当前 广东 深圳 个人网站 发短消息 加为好友 打分 21 专长 JS,PHP,C/C++
4# 大 中 小 发表于 2008-4-2 18:55
我这个,方法不多,但是够用,没有事务切换的方法:
复制内容到剪贴板
代码:
<?php
/**
* 数据库类
*
* Copyright(c) 2005-2008 by 陈毅鑫(深空). All rights reserved
*
* To contact the author write to {@link mailto:shenkong@php.net}
*
* @author 陈毅鑫(深空)
* @version $Id: DB.class.php 50 2008-01-10 20:56:54Z skchen $
* @package 公共组件
*/
abstract class DB {
const DB_FETCH_ASSOC = 1;
const DB_FETCH_ARRAY = 3;
const DB_FETCH_ROW = 2;
const DB_FETCH_DEFAULT = self::DB_FETCH_ASSOC;
public static $db;
protected static $db_type = array('mysqli' => 'MySQLi', 'oracle' => 'Oracle');
protected $u_conn;
protected $q_conn;
protected $dsn;
protected $db_key;
protected $fecth_mode;
protected $sql;
protected $sqls;
protected $qrs;
protected $urs;
protected $u_sqls;
protected $q_sqls;
protected $query_num;
protected $update_num;
public function __construct() {
}
public static function &init(& $dsn, $db_key, $fetch_mode = self::DB_FETCH_ASSOC) {
$key = explode('.', $db_key);
$key = "['" . implode("']['" , $key) . "']";
eval('$flag = isset(self::$db' . $key . ');');
eval("\$db_info = \$dsn" . $key . ";");
if (!$flag) {
$class_name = 'DB_' . self::$db_type[strtolower($db_info['db_type'])];
$obj = new $class_name($db_info, $db_key, $fetch_mode);
eval('self::$db' . $key . ' =& $obj;');
unset($obj);
}
return self::$db;
}
public abstract function connect($type = "slave");
public abstract function close();
public abstract function query($sql, $limit = null, $quick = false);
public abstract function update($sql);
public abstract function getOne($sql);
public abstract function getCol($sql, $limit = null);
public abstract function getRow($sql, $fetch_mode = self::DB_FETCH_DEFAULT);
public abstract function getAll($sql, $limit = null, $fetch_mode = self::DB_FETCH_DEFAULT);
}
class DB_MySQLi extends DB {
public function __construct(& $db_info, $db_key, $fetch_mode) {
$this->db_key = $db_key;
$this->dsn =& $db_info;
$this->fecth_mode = $fetch_mode;
}
public function connect($type = "slave") {
if ($type == "master" || !isset($this->dsn["slave"])) {
$db_host = isset($this->dsn["master"]) ? $this->dsn["master"]["db_host"] : $this->dsn["db_host"];
$db_name = isset($this->dsn["master"]) ? $this->dsn["master"]["db_name"] : $this->dsn["db_name"];
$db_user = isset($this->dsn["master"]) ? $this->dsn["master"]["db_user"] : $this->dsn["db_user"];
$db_pass = isset($this->dsn["master"]) ? $this->dsn["master"]["db_pass"] : $this->dsn["db_pass"];
$this->u_conn = mysqli_connect($db_host, $db_user, $db_pass);
if (!$this->u_conn) {
throw new DB_Exception('更新数据库连接失败');
}
if (!mysqli_select_db($this->u_conn, $db_name)) {
throw new DB_Exception('更新数据库选择失败');
}
if (!isset($this->dsn["slave"])) {
$this->q_conn =& $this->u_conn;
}
} else {
if (empty($this->dsn["slave"])) {
$this->connect('master');
return $this->q_conn =& $this->u_conn;
}
if (empty($_COOKIE[COOKIE_PREFIX . $this->db_key . '_db_no'])) {
$db_no = array_rand($this->dsn["slave"]);
setcookie(COOKIE_PREFIX . $this->db_key . '_db_no', $db_no, null, COOKIE_PATH, COOKIE_DOMAIN);
} else {
$db_no = $_COOKIE[COOKIE_PREFIX . $this->db_key . '_db_no'];
}
$db_info = $this->dsn["slave"][$db_no];
$db_host = $db_info["db_host"];
$db_name = $db_info["db_name"];
$db_user = $db_info["db_user"];
$db_pass = $db_info["db_pass"];
$this->q_conn = mysqli_connect($db_host, $db_user, $db_pass);
if (!$this->q_conn) {
if (!$this->u_conn) {
$this->connect('slave');
}
$this->q_conn =& $this->u_conn;
if (!$this->q_conn) {
throw new DB_Exception('查询数据库连接失败');
}
} else {
if (!mysqli_select_db($this->q_conn, $db_name)) {
throw new DB_Exception('查询数据库选择失败');
}
}
}
return true;
}
public function close() {
if ($this->u_conn === $this->q_conn) {
if (is_object($this->u_conn)) {
mysqli_close($this->u_conn);
}
} else {
if (is_object($this->u_conn)) {
mysqli_close($this->u_conn);
}
if (is_object($this->q_conn)) {
mysqli_close($this->q_conn);
}
}
}
public function query($sql, $limit = null, $quick = false) {
if ($limit != null) {
$sql = $sql . " LIMIT " . $limit;
}
$this->sqls[] = $sql;
$this->q_sqls[] = $sql;
$this->sql = $sql;
if (!$this->q_conn) {
$this->connect("slave");
}
$this->qrs = mysqli_query($this->q_conn, $sql, $quick ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
if (!$this->qrs) {
throw new DB_Exception('查询失败:' . mysqli_error($this->q_conn));
} else {
$this->query_num++;
return $this->qrs;
}
}
public function fetch($rs, $fetch_mode = self::DB_FETCH_DEFAULT) {
switch ($fetch_mode) {
case 1:
$fetch_mode = self::DB_FETCH_ASSOC;
break;
case 2:
$fetch_mode = self::DB_FETCH_ROW;
break;
case 3:
$fetch_mode = self::DB_FETCH_ARRAY;
break;
default:
$fetch_mode = self::DB_FETCH_DEFAULT;
break;
}
return mysqli_fetch_array($rs, $fetch_mode);
}
public function update($sql) {
$this->sql = $sql;
$this->sqls[] = $this->sql;
$this->u_sqls[] = $this->sql;
if (!$this->u_conn) {
$this->connect("master");
}
$this->urs = mysqli_query($this->u_conn, $sql, MYSQLI_USE_RESULT);
if (!$this->urs) {
throw new DB_Exception('更新失败:' . mysqli_error($this->u_conn));
} else {
$this->update_num++;
return $this->urs;
}
}
public function getOne($sql) {
if (!$rs = $this->query($sql, 1, true)) {
return false;
}
$row = $this->fetch($rs, self::DB_FETCH_ROW);
$this->free();
return $row[0];
}
public function getCol($sql, $limit = null) {
if (!$rs = $this->query($sql, $limit, true)) {
return false;
}
$result = array();
while ($rows = $this->fetch($rs, self::DB_FETCH_ROW)) {
$result[] = $rows[0];
}
$this->free();
return $result;
}
public function getRow($sql, $fetch_mode = self::DB_FETCH_DEFAULT) {
if (!$rs = $this->query($sql, 1, true)) {
return false;
}
$row = $this->fetch($rs, $fetch_mode);
$this->free();
return $row;
}
public function getAll($sql, $limit = null, $fetch_mode = self::DB_FETCH_DEFAULT) {
if (!$rs = $this->query($sql, $limit, true)) {
return false;
}
$all_rows = array();
while($rows = $this->fetch($rs, $fetch_mode)) {
$all_rows[] = $rows;
}
$this->free();
return $all_rows;
}
public function rows() {
return mysqli_num_rows($this->qrs);
}
public function lastID() {
return mysqli_insert_id($this->u_conn);
}
public function free() {
mysqli_free_result($this->qrs);
$this->qrs = null;
}
public function escape($str) {
return addslashes($str);
}
public function __destruct() {
}
}
class DB_Exception extends Exception {
}
/*
$db = DB::init($configs['db_info'], 'common', 1);
try {
$rs = $db['common']->query('SELECT * FROM tj_session');
while ($row = $db['common']->fetch($rs)) {
$record[] = $row;
}
} catch (DB_Exception $e) {
print_r($e);
}
*/
?>
分享到:
相关推荐
在这个"MySQL操作类"中,我们很显然看到是为MySQL数据库的操作提供了一套接口或者类库,使得开发者在实际项目中能够更方便地进行数据的存取和管理,而无需直接编写SQL语句。 首先,我们要理解在面向对象编程中,类...
**PHP MySQL操作类详解** 在PHP开发中,与MySQL数据库进行交互是常见的需求。为了方便高效地处理数据库操作,开发者通常会封装一个MySQL操作类,如`PHP_DataSet`。这个类可以提供一系列的方法,包括连接数据库、...
(1)在需要调用mysql操作类的窗口头文件*.h中引入mysql头文件:#include "mysql.h" (2)在需要调用mysql操作类的窗口头文件*.h中声明mysql对象指针: public: mysql *db; (3)在需要调用mysql操作类的窗口...
然而,通过创建自定义的MySQL操作类,我们可以定制自己的接口,使其更适合项目需求。下面,我们将详细讨论此类的常见设计和功能。 1. **类的构造函数**: - 构造函数用于初始化类的实例,通常在这里建立到MySQL...
"MYSQL操作类"是一个专门为MySQL设计的ADO(ActiveX Data Objects)操作类库,它允许开发者通过编程的方式与MySQL数据库进行交互,实现了模板化的连接池功能,以优化数据库连接的管理和复用,同时也提供了扩展性,...
java mysql 操作类 用面向对象的思路封装大部分mysql的操作
在本教程中,我们将探讨“MySQL操作类”的概念,这是一个专门为简化与MySQL数据库交互而设计的编程工具,适合初学者学习。对于经验丰富的开发者来说,这可能是一个基础的资源,但对新手来说,它提供了一个很好的起点...
PHP MYSQL 操作类
MySQL操作类是数据库编程中常用的一种工具,它封装了与MySQL数据库进行交互的函数和方法,使得开发者可以更方便地执行SQL语句、管理数据库连接等任务。在本例中,"MySql操作类(源码).rar"是一个包含C++实现的MySQL...
总结起来,这篇文档展示了如何利用PHP语言来构建一个简单的MySQL操作类,实现了基本的数据库操作功能。通过面向对象的方式封装数据库操作,可以大大简化代码的重复编写,并提高代码的可维护性和可重用性。这在实际的...
这款PHP MySQL操作类提供了全面的功能,涵盖了数据的查询、更新、删除等操作,适用于初学者进行学习和实践,同时也为有经验的开发者提供了一个可自定义的基础框架。下面我们将详细讨论其中涉及的关键知识点。 1. **...
"php数据库mysql操作类"就是这样的一个工具,它集成了多种数据库操作方法,旨在简化开发流程。 首先,这个数据库操作类通常会包含连接和断开数据库的功能。在PHP中,可以通过mysqli或PDO的构造函数建立到MySQL...
在这个案例中,我们关注的是"MySql操作类"和"MySql.Data.dll"。 `MySql.Data.dll`是MySQL官方提供的.NET数据提供程序,它使得C#开发者能够方便地连接到MySQL服务器,执行SQL查询,以及处理数据。这个DLL包含了所有...
PHP数据库MySql操作类通常是为了简化数据库交互,提高代码的可读性和可维护性而设计的。这样的类会封装常见的数据库操作,如连接、查询、插入、更新和删除等,使得开发者无需直接编写SQL语句,而是通过调用类方法来...
MySQL操作类是编程中用于与MySQL数据库交互的工具,它封装了各种数据库操作,使得开发者可以更方便地执行SQL语句、管理数据、查询记录等。在这个特定的案例中,这个操作类是用Visual C++(简称VC)编写的,这在C++...
我仿照php的mysqli扩展自己使用MySQL的C Interface实现了一个简单的mysql操作类。这个类可以帮助我更方便地与MySQL数据库进行交互,并且让我更好地理解底层的操作原理。基于这个类,我写了一个简单的字符统计测试...
MySQL操作类是数据库编程中常用的一种工具,它用于简化对MySQL数据库的访问,尤其是在使用C++编程时。在这个场景中,我们讨论的类是对MySQL C API的封装,它提供了更高级别的接口,使得开发人员可以方便地进行数据的...
介绍一个简单的php-mysql操作类,数据库连接和设置,执行操作,显示信息,取得数据集的某个值,取得数据集的某个值,取得数据集的行数,循环读取数据,最后一次插入纪录的id值,插入任意数据,修改数据,删除数据,...