Phpmysql database class _ PHP Tutorial

Source: Internet
Author: User
Tags pconnect
Phpmysql database class. This is a common mysql database class. download it. ClassmySqlClass {defines the entire database category class mySqlClassvar $ querynum0; the query count variable is a common mysql database class. download it and check it.

Class mySqlClass {// define the entire database metadata class mySqlClass
Var $ querynum = 0; // variable of the number of queries

// Open the connection to the server ------------------------------------------------------------------------------------------------------//
Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ pconnect = 0 ){
// Mysql_connect -- open a connection to the MySQL server
// Mysql_pconnect -- open a persistent connection to the MySQL server
// Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences.
// First, this function will first try to find a (persistent) connection that has been enabled with the same user name and password on the same host. If yes, the connection id is returned without a new connection.
// Second, after the script is executed, the connection to the SQL Server will not be closed, and the connection will remain open for future use (mysql_close () will not be closed by mysql_pconnect () established connections ).
// Determine whether to enable persistent connection. If yes, use persistent connection. If no persistent connection is enabled, use a general connection.
If ($ pconnect ){
If (! @ Mysql_pconnect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('1 ');
}
} Else {
If (! @ Mysql_connect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('1 ');
}
}
// Set the character encoding if the database version is later than 4.1 and the character set parameter in the global variable is not empty ---------------------------------------------------------//
If ($ this-> version ()> '4. 1 '){
// $ Charset = 'gbk ';
$ Charset = 'utf-8 ';
$ Dbcharset = '';
If (! $ Dbcharset & in_array (strtolower ($ charset), array ('gbk', 'big5', 'utf-8 '))){
$ Dbcharset = str_replace ('-', '', $ charset );
}
// Mysql_query -- send a MySQL Query
// Set the NAMES parameter to the parameter value of the corresponding character set to avoid Chinese garbled characters when PHP reads the mysql database.
If ($ dbcharset ){
Mysql_query ("SET character_set_connection = $ dbcharset, character_set_results = $ dbcharset, character_set_client = binary ");
}
// If the database version is later than 5.0, set the SQL _mode parameter to null.
If ($ this-> version ()> '5. 0.1 '){
Mysql_query ("SET SQL _mode = ''");
}
}
If ($ dbname) {// if the database name exists
// Mysql_select_db -- select MySQL database
// If the selected database fails
Mysql_select_db ($ dbname );
}
}

// Mysql_select_db -- select MySQL database ---------------------------------------------------------------------//
Function select_db ($ dbname ){
Return mysql_select_db ($ dbname );
}

// Read a data record function ---------------------------------------------------------------------//
// Mysql_fetch_array -- get a row from the result set as an associated array, or an array of numbers, or both
// The Second Optional parameter result_type in mysql_fetch_array () is a constant and can accept the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.
// The Data column returned by MYSQL_ASSOC uses the field name as the index name of the array.
// The Data column returned by MYSQL_BOTH uses the field name and numeric index as the index name of the array.
// The Data column returned by MYSQL_NUM uses the numeric index as the index name of the array. The index starts from 0, indicating the first field in the returned result.
Function fetch_array ($ query, $ result_type = MYSQL_ASSOC ){
Return mysql_fetch_array ($ query, $ result_type );
}

// Query database functions ---------------------------------------------------------------------//
Function query ($ SQL, $ type = ''){
$ Func = $ type = 'unbuffered' & @ function_exists ('MySQL _ unbuffered_query ')?
'MySQL _ unbuffered_query ': 'MySQL _ query ';
If (! ($ Query = $ func ($ SQL) & $ type! = 'Silent '){
$ This-> halt ('0 ');
}
// Query times + 1
$ This-> querynum ++;
// Return the query result resource
Return $ query;
}

// Return the number of affected records ---------------------------------------------------------------------//
// If the function is successfully executed, the number of records is returned. if the function fails, "-1" is returned ".
Function affected_rows (){
Return mysql_affected_rows ();
}

// Return the text error message generated by the previous MySQL operation ---------------------------------------------//

Function error (){
Return mysql_error ();
}

// Return the text error message generated by the previous MySQL operation integer type ------------------------------------//

Function errno (){
Return intval (mysql_errno ());
}

// Mysql_result -- field value in the returned result set ---------------------------------------------//

Function result ($ query, $ row ){
$ Query = @ mysql_result ($ query, $ row );
Return $ query;
}

// Mysql_num_rows -- obtain the number of rows in the result set ---------------------------------------------//

Function num_rows ($ query ){
$ Query = mysql_num_rows ($ query );
Return $ query;
}

// Mysql_num_fields -- obtain the number of fields in the result set ---------------------------------------------//

Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}

// Mysql_free_result -- release the result memory ---------------------------------------------//

Function free_result ($ query ){
Return mysql_free_result ($ query );
}

// Mysql_insert_id -- obtain the ID generated by the previous INSERT operation ---------------------------------------------//

Function insert_id (){
$ Id = mysql_insert_id ();
Return $ id;
}

// Mysql_fetch_row -- obtain a row from the result set as a numeric array ---------------------------------------------//

Function fetch_row ($ query ){
$ Query = mysql_fetch_row ($ query );
Return $ query;
}

// Mysql_field_name -- obtain the field name of the specified field in the result ---------------------------------------------//

Function field_name ($ query, $ num = 0 ){
Return mysql_field_name ($ query, $ num );
}

// Mysql_fetch_field -- obtains the column information from the result set and returns $ num = 0 as the object, indicating the first field -------------------------------//

Function fetch_fields ($ query, $ num = 0 ){
Return mysql_fetch_field ($ query, $ num );
}

// Mysql_get_server_info -- obtain MySQL server Information ---------------------------------------------//

Function version (){
$ MySqlVer = mysql_get_server_info ();
If (empty ($ MySqlVer) | $ MySqlVer = ""){
$ MySqlVer = $ this-> result ($ this-> query ("select version ()"), 0 );
}
If (empty ($ MySqlVer) | $ MySqlVer = ""){
$ MySqlVer = '0 ';
}
Return $ MySqlVer;
}


// Mysql_close -- close the MySQL connection ---------------------------------------------------------------------//

Function close (){
Return @ mysql_close ();
}


// Exception handling function halt ---------------------------------------------------------------------//

Function halt ($ mess ){
Global $ DataBaseBadFlag;
// $ Dberror = $ this-> error ();
// $ Dberrno = $ this-> errno (); // The upper limit of 1114 person connections
If ($ mess = "1 "){
If ($ DataBaseBadFlag! = 1 ){
Echo (" ");
}
Else {
If (! @ Function_exists ('phpescape ')){
Require ("escape. php ");
}
Echo phpEscape ("Lock = false; location. href = 'pagedataerr. php ';");
}
Exit ();
}
Else {
Return false;
}
}
}


Bytes. Class mySqlClass {// define the entire database category class mySqlClass var $ querynum = 0; // The number of queries variable // open...

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.