Copy CodeThe code is as follows:
/*
* MySQL Database connection class
* mysql.class.php 2009.04.15 by Hackbaby
*/
Class Dbstuff {
var $version = ';
var $querynum = 0;
var $link = null;
Connecting to a database
Function Connect ($dbhost, $dbuser, $DBPW, $dbname, $pconnect = 0, $halt = TRUE, $dbcharset 2 = ") {
$conn = Empty ($pconnect)? ' Mysql_connect ': ' Mysql_pconnect ';
if (! $this->link = @ $conn ($dbhost, $dbuser, $DBPW, 1)) {
$halt && $this->halt (' Can not connect to MySQL server ');
} else {
if ($this->version () > ' 4.1 ') {
Global $charset, $dbcharset;
$dbcharset = $dbcharset 2? $dbcharset 2: $dbcharset;
$dbcharset =! $dbcharset && in_array (Strtolower ($charset), Array (' GBK ', ' Big5 ', ' utf-8 ')? Str_replace ('-', ', $charset): $dbcharset;
$serverset = $dbcharset? ' character_set_connection= '. $dbcharset. ', character_set_results= ' $dbcharset. ', character_set_client=binary ': ';
$serverset. = $this->version () > ' 5.0.1 '? (Empty ($serverset)? '' : ',').' Sql_mode=\ ' \ '): ';
$serverset && mysql_query ("SET $serverset", $this->link);
}
$dbname && @mysql_select_db ($dbname, $this->link);
}
}
Select Database
function select_db ($dbname) {
Return mysql_select_db ($dbname, $this->link);
}
How to return a database after execution
function Fetch_array ($query, $result _type = Mysql_assoc) {
Return mysql_fetch_array ($query, $result _type);
}
Returns the return value after the Fetch_array method executes
function Fetch_first ($sql) {
return $this->fetch_array ($this->query ($sql));
}
Returns the return value after the result method executes
function Result_first ($sql) {
return $this->result ($this->query ($sql), 0);
}
Performing operations on a database
function query ($sql, $type = ") {
Global $debug, $discuz _starttime, $sqldebug, $sqlspenttimes;
Determine Debug Debug
if (defined (' Sys_debug ') && sys_debug) {
@include_once web_root. " /include/debug.func.php ';
SQLDebug ($sql);
}
//
$func = $type = = ' unbuffered ' && @function_exists (' Mysql_unbuffered_query ')?
' Mysql_unbuffered_query ': ' mysql_query ';
if (! ( $query = $func ($sql, $this->link))) {
if (In_array ($this->errno (), Array (2006)) && substr ($type, 0, 5)! = ' RETRY ') {
$this->close ();
Require './config.inc.php ';
$this->connect ($dbhost, $dbuser, $DBPW, $dbname, $pconnect, True, $dbcharset);
$this->query ($sql, ' RETRY '. $type);
} elseif ($type! = ' SILENT ' && substr ($type, 5)! = ' SILENT ') {
$this->halt (' MySQL Query Error ', $sql);
}
}
Query bar number plus 1
$this->querynum++;
return $query;
}
Gets the number of record rows affected by the previous MySQL operation
function Affected_rows () {
Return Mysql_affected_rows ($this->link);
}
Text error message for database
Function error () {
Return ($this->link) mysql_error ($this->link): Mysql_error ());
}
Returns the numeric encoding of the error message in the previous MySQL operation
function errno () {
Return Intval ($this->link) Mysql_errno ($this->link): Mysql_errno ());
}
Returns the results of a database operation
function result ($query, $row = 0) {
$query = @mysql_result ($query, $row);
return $query;
}
Returns the number of operations such as database queries
function Num_rows ($query) {
$query = mysql_num_rows ($query);
return $query;
}
Get the number of fields in the result set
function Num_fields ($query) {
Return Mysql_num_fields ($query);
}
Releasing the resulting memory
function Free_result ($query) {
Return Mysql_free_result ($query);
}
Gets the ID generated by the INSERT operation in the previous step
function insert_id () {
return ($id = mysql_insert_id ($this->link)) >= 0? $id: $this->result ($this->query ("Select last_insert_id ()"), 0);
}
Returns the query result as an array
function Fetch_row ($query) {
$query = Mysql_fetch_row ($query);
return $query;
}
Get column information from the result set and return as an object
function Fetch_fields ($query) {
Return Mysql_fetch_field ($query);
}
Get the version of MySQL
Function version () {
if (Empty ($this->version)) {
$this->version = mysql_get_server_info ($this->link);
}
return $this->version;
}
Close connection
function Close () {
Return Mysql_close ($this->link);
}
Error hints
function Halt ($message = ', $sql = ') {
Define (' Cache_forbidden ', TRUE);
Require_once Web_root. './include/db_mysql_error.inc.php ';
}
}
?>
http://www.bkjia.com/PHPjc/320171.html www.bkjia.com true http://www.bkjia.com/PHPjc/320171.html techarticle Copy the code as follows: PHP/* * MySQL Database connection class * mysql.class.php 2009.04.15 by Hackbaby */class Dbstuff {var $version = '; var $q Uerynum = 0; var $link = null; Even ...