MySQL database category
Last Update:2018-12-08
Source: Internet
Author: User
/**
* @ Purpose: MySQL database category
* @ Package:
* @ Author: lisen@sellingclub.cn
* @ Modifications:
* @ See:
* @ Time: 2008.10.10
*/
Class db_mysql
{
// ================================================ ======================================
Private $ host = 'localhost ';
Private $ database = 'db _ name ';
Private $ user = 'user ';
Private $ Password = 'Password ';
// ================================================ ======================================
Private $ link_id = 0; // database connection
Private $ query_id = 0; // query result
Private $ row_result = array (); // an array composed of result sets
Private $ field_result = array (); // an array composed of result set Fields
Private $ affected_rows; // Number of affected rows
Private $ rows; // number of rows recorded in the result set
Private $ fields; // number of fields in the result set
Private $ row_postion = 0; // record pointer Position Index
Public $ insert_id = 0;
//************************************** **********************
/***** Constructor ****/
Function _ construct ()
{
$ This-> connect ();
}
/***** Destructor ****/
Function _ destruct ()
{
@ Mysql_free_result ($ this-> query_id );
Mysql_close ($ this-> link_id );
}
/***** Connect to the server and select the database ****/
Function connect ($ database = '', $ host ='', $ user = '', $ Password = '')
{
$ Database = ''? $ This-> Database: $ database;
$ Host = ''? $ This-> HOST: $ host;
$ User = ''? $ This-> User: $ user;
$ Password = ''? $ This-> password: $ password;
//-----------------------------------------------------------//
If (0 = $ this-> link_id)
{
$ This-> link_id = @ mysql_pconnect ($ host, $ user, $ password );
If (! $ This-> link_id)
{
$ This-> halt ('failed to connect to the database server! ');
}
If (! Mysql_select_db ($ this-> database, $ this-> link_id ))
{
$ This-> halt ('the specified database cannot be opened:'. $ this-> database );
}
}
Return $ this-> link_id;
}
/***** Release memory ****/
Function free ()
{
If (@ mysql_free_result ($ this-> query_id ))
{
Unset ($ this-> row_result );
}
$ This-> query_id = 0;
}
/***** Execute the query ****/
Function query ($ QUERY_STRING)
{
// Release the memory of the last Query
If ($ this-> query_id)
{
$ This-> free ();
}
If (0 = $ this-> link_id)
{
$ This-> connect ();
}
// Set the Chinese Character Set
@ Mysql_query ('set names gb2312 ');
$ This-> query_id = mysql_query ($ QUERY_STRING, $ this-> link_id );
$ This-> insert_id = mysql_insert_id ();
If (! $ This-> query_id)
{
$ This-> halt ('SQL query statement error:'. $ QUERY_STRING );
}
@ Mysql_query ('set names gb2312 ');
Return $ this-> query_id;
}
/***** Direct the result set pointer to the specified row ****/
Function seek ($ POS)
{
If (@ mysql_data_seek ($ this-> query_id, $ POS ))
{
$ This-> row_position = $ Pos;
Return true;
}
Else
{
$ This-> halt ('locate the result set error! ');
Return false;
}
}
/***** Array composed of returned result sets ****/
Function get_rows_array ()
{
$ This-> get_rows ();
For ($ I = 0; $ I <$ this-> rows; $ I ++)
{
If (! Mysql_data_seek ($ this-> query_id, $ I ))
{
$ This-> halt ('mysql _ data_seek query error! ');
}
$ This-> row_result [$ I] = mysql_fetch_array ($ this-> query_id );
}
Return $ this-> row_result;
}
/***** An array composed of returned result set fields ****/
Function get_fields_array ()
{
$ This-> get_fields ();
For ($ I = 0; $ I <$ this-> fields; $ I ++)
{
$ OBJ = mysql_fetch_field ($ this-> query_id, $ I );
$ This-> field_result [$ I] = $ obj-> name;
}
Return $ this-> field_result;
}
/***** Number of affected records returned ****/
Function get_affected_rows ()
{
$ This-> affected_rows = mysql_affected_rows ($ this-> link_id );
Return $ this-> affected_rows;
}
/***** Number of records in the returned result set ****/
Function get_rows ()
{
$ This-> rows = mysql_num_rows ($ this-> query_id );
Return $ this-> rows;
}
/***** Number of fields in the returned result set ****/
Function get_fields ()
{
$ This-> fields = mysql_num_fields ($ this-> query_id );
Return $ this-> fields;
}
/***** Execute the SQL statement and return an array consisting of the first row of records in the query results ****/
Function fetch_one_array ($ SQL)
{@ Mysql_query ('set names gb2312 ');
$ This-> query ($ SQL );
Return mysql_fetch_array ($ this-> query_id );
}
/***** Print the error message ****/
Function halt ($ MSG)
{
$ This-> error = mysql_error ();
Printf ("<font style = 'font-family: Arial, al; font-size: 12px; '> <B> database error: </B> % s \ n ", $ MSG );
Printf ("error message returned by MySQL: </B> % s \ n", $ this-> error );
Printf ("error page: <font style = 'color: # effecee; text-Decoration: underline'> % S </font> \ n ", $ _ server ['php _ Self ']);
Printf ("Please submit the error message to the system administrator or website Program Personnel handling! \ N ");
Die ('<B> <font color = Red> script termination </font> </B> </font> ');
}
}