<?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);
}
*/
?>
分享到:
相关推荐
PHP MYSQL操作类 PHP_DataSet
(1)在需要调用mysql操作类的窗口头文件*.h中引入mysql头文件:#include "mysql.h" (2)在需要调用mysql操作类的窗口头文件*.h中声明mysql对象指针: public: mysql *db; (3)在需要调用mysql操作类的窗口...
java mysql 操作类 用面向对象的思路封装大部分mysql的操作
对MySQL的各种操作进行了封装,工程中只需要调用相应的接口即可。
PHP MYSQL 操作类
MYSQL ADO操作类,模板化连接池,可增加或替换数据库类型
比较方便的一个类,分享了 比较适合初中级开发人员,比较实用
我仿照php的mysqli扩展自己使用MySQL的C Interface实现了一个简单的mysql操作类。这个类可以帮助我更方便地与MySQL数据库进行交互,并且让我更好地理解底层的操作原理。基于这个类,我写了一个简单的字符统计测试...
介绍一个简单的php-mysql操作类,数据库连接和设置,执行操作,显示信息,取得数据集的某个值,取得数据集的某个值,取得数据集的行数,循环读取数据,最后一次插入纪录的id值,插入任意数据,修改数据,删除数据,...
C++编写的MySql操作类(源码),是“忽悠张”写的东东,跑了一下,能够满足基本的MYSQL操作.如果对写服务器有兴趣可以看看这个博客:www.doserver.com
一款比较全的mysql操作类,这个是一款包括数据查询,更新,删除,等操作,有需要的小伙伴可以下载学习和参考,也可以自己进行修改操作,达到自己的目的。
这个是连接.net 连接mysql的操作类,希望大家用得上,也是自己好不容易找到的
理想的mysql操作类,网上找了很久,建议下载参考
最近用到Python操作数据库,MySql的操作封装类,简单写了一个Mysql封装操作类,分享一下,需要安装pymsql,安装方法:https://blog.csdn.net/sinat_28984567/article/details/105316092
新手学习,php7中mysql数据库操作类实例,非常清晰易懂
介绍一个不错的PHP mysql操作类,SQL执行后的数据保存变量,读取一条数据并把数据往后移一位,如果数据为空则返回为null,更新数据库里面的数据,$t为表名,$v格式为数组格式,上标为字段名,下标为数据;$w为条件上标...
浅谈PHP值mysql操作类_.docx
Mysql操作类,含常用数据库处理功能,用VC编写
用mysql有关C语言的API函数封装了自己的mysql操作类,包括了所有常用的功能,使用简单方便,包含使用说明和实例程序。