MySQL connection class

Source: Internet
Author: User
Tags pconnect
<?

Class Mysql {
Var $ linkid;
Var $ db_host;
Var $ db_user;
Var $ db_pwd;
Var $ db_name;
Var $ querynum = 0;
Var $ selectid;
Var $ result = null;
Var $ pconnect = true;

// Constructor. Database parameters must be included in class initialization.
Function Mysql ($ db_host, $ db_user, $ db_pwd, $ db_name, $ pconnect = true ){
$ This-> db_host = $ db_host;
$ This-> db_user = $ db_user;
$ This-> db_pwd = $ db_pwd;
$ This-> db_name = $ db_name;

If ($ this-> pconnect ==$ pconnect ){
$ This-> linkid = @ mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
}
Else {
$ This-> linkid = @ mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
}

If ($ this-> linkid ){
If ($ this-> db_name! = ""){
$ Dbselect = @ mysql_select_db ($ this-> db_name) or $ this-> mysql_err ("database not exists ");
}
}
Else {
$ This-> mysql_err ("cannot connect the database server, pls chk your password ");
}
// $ This-> query ("set names 'utf8 '");
}

// Execute SQL statements
Function query ($ SQL ){
$ This-> result = @ mysql_query ($ SQL, $ this-> linkid) or $ this-> mysql_err ("SQL statement". $ SQL. "error ");
$ This-> querynum ++;
Return $ this-> result;
}

Function fetch_array ($ SQL ){
Return mysql_fetch_array ($ SQL, MYSQL_ASSOC );
}

// Used to count the number of records
Function Number ($ SQL ){
$ This-> result = $ this-> query ($ SQL );
$ Number = mysql_num_rows ($ this-> result );
$ This-> free ();
Return $ number;
}

// Obtain a record
Function getrow ($ SQL ){
$ This-> query ($ SQL );
$ Row = mysql_fetch_array ($ this-> result, MYSQL_ASSOC );
$ This-> Free ();
Return $ row;
}
// Retrieve all records
Function GetRows ($ SQL ){
$ This-> result = $ this-> query ($ SQL );
While ($ ROW = mysql_fetch_array ($ this-> result, mysql_assoc ))
{
$ Allrows [] = $ row;
}
$ This-> Free ();
Return $ allrows;
}

// Obtain the ID field of the current insert
Function InsertID (){
$ Id = mysql_insert_id ();
Return $ id;
}
// Close the database
Function Close (){
@ Mysql_close ($ this-> linkid );
}
// Print the error message
Function Mysql_err ($ msg ){
Echo $ msg;
Exit;
}
// Release
Function Free (){
@ Mysql_free_result ($ this-> result );
$ This-> result = null;
}

}
?>

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.