<?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:[email protected]} * * @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); } */ ?>