Home  >  Article  >  Backend Development  >  MySQL database operation class under PHP (modified from discuz)_PHP tutorial

MySQL database operation class under PHP (modified from discuz)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:381192browse

Copy code The code is as follows:


/* --------------------------------
System:PT book - PT小说小偷
Code: 杰少Pakey
----------------------------------- */

$pt_mysql = new dbQuery;

/**
* mysql query class
*
*/

class dbQuery {
/**
* Total number of queries
*
* @var int
*/
var $querynum = 0;
/**
* Connection handle
*
* @var object
*/
var $link;

/**
* Constructor
*
* @param string $dbhost host name
* @param string $dbuser user
* @param string $dbpw password
* @param string $dbname Database name
* @param int $pconnect Whether to continue connecting
*/
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0) {
if($pconnect) {
if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
$this->halt('Can not connect to MySQL server');
}
} else {
if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
$this->halt('Can not connect to MySQL server');
}
}
if($this->version() > '4.1') {
global $dbcharset;
if($dbcharset) {
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
mysql_query("set names 'GBK'");
}

if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''", $this->link);
mysql_query("set names 'GBK'");
}
}

if($dbname) {
mysql_select_db($dbname, $this->link);
}

}
/**
* Select database
*
* @param string $dbname
* @return
*/
function select_db($dbname) {
return mysql_select_db($dbname, $this->link);
}
/**
* Get a record from the result set
*
* @param object $query
* @param int $result_type
* @return array
*/
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}

/**
* 查询SQL
*
* @param string $sql
* @param string $type
* @return object
*/
function query($sql, $type = '') {

$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
$this->halt('MySQL Query Error', $sql);
}

$this->querynum++;
return $query;
}
/**
* Get the number of affected items
*
* @return int
*/
function affected_rows() {
return mysql_affected_rows($this->link);
}
/**
* Return error message
*
* @return array
*/
function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}
/**
* Return error code
*
* @return int
*/
function errno() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
}
/**
* Return query results
*
* @param object $query
* @param string $row
* @return mixed
*/
function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}
/**
* Number of results
*
* @param object $query
* @return int
*/
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
/**
* Get the total number of fields
*
* @param object $query
* @return int
*/
function num_fields($query) {
return mysql_num_fields($query);
}
/**
* Release the result set
*
* @param object $query
* @return bool
*/
function free_result($query) {
return mysql_free_result($query);
}
/**
* Return auto-increment ID
*
* @return int
*/
function insert_id() {
return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
/**
* Get a row from the result set as an enumeration array
*
* @param object $query
* @return array
*/
function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
/**
* Get column information from the result set and return it as an object
*
* @param object $query
* @return object
*/
function fetch_fields($query) {
return mysql_fetch_field($query);
}
/**
* Return mysql version
*
* @return string
*/
function version() {
return mysql_get_server_info($this->link);
}
/**
* Close connection
*
* @return bool
*/
function close() {
return mysql_close($this->link);
}
/**
* Output error message
*
* @param string $message
* @param string $sql
*/
function halt($message = '', $sql = '') {
echo $message . ' ' . $sql;
exit;

}
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322101.htmlTechArticleCopy the code as follows: ?php /* --------------- ------------------ System: PT book - PT novel thief Code: Jie Shao Pakey ------------------ --------------- */ $pt_mysql = new dbQuery...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn