mysql_class.php
资源名称:BBWPS.rar [点击查看]
上传用户:yttaitong
上传日期:2009-05-10
资源大小:128k
文件大小:4k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
PHP
- <?php
- /*
- [BBWPS!] (C)2006-2010 小蜜蜂版权所有.
- This is NOT a freeware, use is subject to license terms
- 时间:2007年12月
- 描述:封装的MYSQL数据库
- */
- Class DB {
- var $query_num = 0; //执行的进程数
- var $connect; //数据库服务器连接
- var $query; //执行返回的结果集
- var $dbhost="localhost"; //数据库服务器
- var $dbuser="root"; //连接用户名
- var $dbpw="123"; //连接密码
- var $dbname; //数据库名称
- var $pconnect=0; //是否支持永久连接
- var $resultOfArray=array(); //查询结果是数组
- var $resultOfObject; //查询结果是对象
- var $charset="GBK"; //数据库字符集
- //初始化数据库连接
- function DB($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset="GBK") {
- $this->dbhost=$dbhost;
- $this->dbuser=$dbuser;
- $this->dbpw=$dbpw;
- $this->dbname=$dbname;
- $this->pconnect=$pconnect;
- $this->charset=$charset;
- $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->pconnect);
- }
- //注销该对象
- function __DB(){
- unset($this->resultOfArray);
- unset($this->resultOfObject);
- $this->close();
- $this->free();
- unset($this->dbhost);
- unset($this->dbname);
- unset($this->pconnect);
- unset($this->dbpw);
- unset($this->query);
- unset($this->query_num);
- unset($this->connect);
- unset($this->dbhost);
- }
- //连接数据库服务器
- function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
- $this->pconnect==0 ? $this->connect=@mysql_connect($dbhost, $dbuser, $dbpw) : $this->connect=@mysql_pconnect($dbhost, $dbuser, $dbpw);
- mysql_errno()!=0 && $this->halt("Connect($pconnect) to MySQL ($dbhost,$dbuser) failed");
- if(mysql_get_server_info() > '4.1'){
- @mysql_query("SET NAMES '$this->charset'",$this->connect);
- }
- if(mysql_get_server_info() > '5.0') {
- @mysql_query("SET sql_mode=''",$this->connect);
- }
- if($dbname) {
- if (!@mysql_select_db($dbname,$this->connect)){
- $this->halt('Cannot use database '.$dbname);
- }
- }
- }
- //关闭数据库服务器连接
- function close() {
- return mysql_close($this->connect);
- }
- //释放内存,注销结果集
- function free(){
- return @mysql_free_result($this->query);
- }
- //选择数据库
- function select_db($dbname){
- if (!@mysql_select_db($dbname,$this->connect)){
- $this->halt('Cannot use database '.$dbname);
- }
- }
- //执行数据库查询
- function query($SQL,$cache) {
- $query = mysql_query($SQL,$this->connect);
- $this->query_num++;
- if($cache){
- $this->query=$query;
- }
- if (!$query) $this->halt('Query Error: ' . $SQL.mysql_error());
- return $query;
- }
- //获得一行数据,返回形式是对象
- //快速的获得一行记录,结果集不保存
- function get_one($SQL){
- $query = mysql_query($SQL,$this->connect);
- $this->query_num++;
- if (!$query) $this->halt('Update Error: ' . $SQL);
- return @mysql_fetch_object($query);
- }
- //更新的时候并不需要把结果集保存
- //和查询有点不一样但是和写入是一样的
- function update($SQL) {
- if(function_exists('mysql_unbuffered_query')){
- $query = mysql_unbuffered_query($SQL,$this->connect);
- }
- else{
- $query = mysql_query($SQL,$this->connect);
- }
- $this->query_num++;
- if (!$query) $this->halt('Update Error: ' . $SQL);
- return true;
- }
- function insert($SQL){
- return $this->update($SQL);
- }
- function fetch_array($result=null,$result_type = MYSQL_ASSOC) {
- if($result==null){
- $this->resultOfArray=@mysql_fetch_array($this->query, $result_type);
- }
- else{
- $this->resultOfArray=@mysql_fetch_array($result, $result_type);
- }
- return $this->resultOfArray;
- }
- function fetch_object(){
- $this->resultOfObject=@mysql_fetch_object($this->query);
- return $this->resultOfObject;
- }
- function affected_rows() {
- return mysql_affected_rows();
- }
- function num_rows() {
- $rows = mysql_num_rows($this->query);
- return $rows;
- }
- function insert_id() {
- $id = mysql_insert_id();
- return $id;
- }
- function halt($msg='') {
- exit($msg);
- }
- }
- ?>