Home >Backend Development >PHP Tutorial >Mysql database Mysql database operation class under PHP is modified from discuz
复制代码 代码如下:
/* --------------------------------
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 persist the connection
*/
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_c 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;
}
}
?>
The above introduces the Mysql database. The mysql database operation class under PHP is modified from discuz, including the content of the Mysql database. I hope it will be helpful to friends who are interested in PHP tutorials.