Mysql database operations in php (Modified from discuz)

Source: Internet
Author: User
Tags pconnect

Copy codeThe Code is as follows:
<? Php

/*--------------------------------
System: PT book-PT novel thief
Code: James 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
* @ Param string $ dbpw Password
* @ Param string $ dbname Database Name
* @ Param int $ whether pconnect is continuously connected
*/
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 a database
*
* @ Param string $ dbname
* @ Return
*/
Function select_db ($ dbname ){
Return mysql_select_db ($ dbname, $ this-> link );
}
/**
* Retrieve 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 );
}

/**
* Querying 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;
}
/**
* Number of affected items
*
* @ Return int
*/
Function affected_rows (){
Return mysql_affected_rows ($ this-> link );
}
/**
* Error message returned
*
* @ 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;
}
/**
* Total number of fields
*
* @ Param object $ query
* @ Return int
*/
Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}
/**
* Release result set
*
* @ Param object $ query
* @ Return bool
*/
Function free_result ($ query ){
Return mysql_free_result ($ query );
}
/**
* The auto-increment ID is returned.
*
* @ Return int
*/
Function insert_id (){
Return ($ id = mysql_insert_id ($ this-> link)> = 0? $ Id: $ this-> result ($ this-> query ("SELECT last_insert_id ()"), 0 );
}
/**
* Retrieve a row from the result set as an enumerated Array
*
* @ Param object $ query
* @ Return array
*/
Function fetch_row ($ query ){
$ Query = mysql_fetch_row ($ query );
Return $ query;
}
/**
* Retrieve 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 );
}
/**
* Returns the mysql version.
*
* @ Return string
*/
Function version (){
Return mysql_get_server_info ($ this-> link );
}
/**
* Close the 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;

}
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.