/**
* @ 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 programmer! \ N ");
Die ('<B> <font color = red> script termination </font> </B> </font> ');
}
}