What we usually see most is the php connection to mysql class. today we will share with you a class for connecting php to SQL server. For more information, see.
Class DB_Handle {
- Var $ ClassName = "DB_Handle ";
- Var $ Server;
- Var $ UserName;
- Var $ Password;
- Var $ Database;
- Var $ LinkID = 0;
- Var $ QueryResult = "";
- Var $ LastInsertID = "";
- /* Private ignore => ignore the error and continue, halt => report the error and halt, report => report the error and continue */
- Var $ Halt_On_Error = "report ";
- Var $ Error = "";
- Var $ ErrNo = 0;
- /** Public
- * Remark: This is the db_mysql_class's structure
- * Function: Set the server, username, password, database variable.
- */
- Function DB_Handle ($ server = "", $ username = "", $ password = "", $ database = ""){
- $ This-> Server = $ server;
- $ This-> UserName = $ username;
- $ This-> Password = $ password;
- $ This-> Database = $ database;
- }
- /** Public
- * Function: Connect database and select database
- * Success: retun 1
- * Failed: return 0
- */
- Function connect (){
- $ This-> LinkID = @ mssql_pconnect ($ this-> Server, $ this-> UserName, $ this-> Password );
- If (! $ This-> LinkID ){
- $ This-> halt ("mssql_pconnect ($ this-> Server, $ this-> UserName, $ this-> Password): Failed ");
- Return 0;
- }
- If (! @ Mssql_select_db ($ this-> Database )){
- $ This-> halt ("mssql_select_db ($ this-> Database) Failed .");
- Return 0;
- }
- Return 1;
- }
- /** Public
- * Function: Check the database, if exist then select
- * Exist: return 1
- * Not exist: return 0
- */
- Function selectDatabase (){
- If (@ mssql_select_db ($ this-> Database ))
- Return 1;
- Else
- Return 0;
- }
- /** Public
- * Function: Execute SQL instruction
- * Success: return SQL Result.
- * Failed: return 0;
- */
- Function execQuery ($ SQL = ""){
- $ This-> connect ();
- If ($ this-> LinkID = 0 ){
- $ This-> halt ("Execute SQL Failed: Have not valid database connect .");
- Return 0;
- }
- Ob_start ();
- $ This-> QueryResult = mssql_query ($ SQL, $ this-> LinkID );
- $ Error = ob_get_contents ();
- Ob_end_clean ();
- If ($ error ){
- $ This-> halt ("Execute SQL: mssql_query ($ SQL, $ this-> LinkID) failed .");
- Return 0;
- }
- $ Reg = "# insert #";
- If (preg_match ($ reg, $ SQL )){
- $ SQL = "select @ IDENTITY as id ";
- $ Res = mssql_query ($ SQL, $ this-> LinkID );
- $ This-> LastInsertID = mssql_result ($ res, 0, id );
- }
- Return $ this-> QueryResult;
- }
-
- /** Public
- * Function: Get the query result's row number
- * Success: return the row fo the Result
- * Failed: return 0
- */
- Function getTotalRowNum ($ result = ""){
- If ($ result! = "")
- $ This-> QueryResult = $ result;
- $ Row = @ mssql_num_rows ($ this-> QueryResult );
- If ($ row> = 0)
- Return $ row;
- $ This-> halt ("Get a row of result Failed: Result $ result is invalid .");
- Return 0;
- }
-
- /** Public
- * Function: Get the last insert record's id
- * Success: return id
- * Failed: return 0
- */
- Function lastInsertID (){
- Return $ this-> LastInsertID;
- }
-
- /** Public
- * Function: Get a field's value
- * Success: return value of the field
- * Failed: return 0
- */
- Function getField ($ result = "", $ row = 0, $ field = 0 ){
- If ($ result! = "")
- $ This-> QueryResult = $ result;
- $ Fieldvalue = @ mssql_result ($ this-> QueryResult, $ row, $ field );
- If ($ fieldvalue! = "")
- Return $ fieldvalue;
- $ This-> halt ("Get field: mssql_result ($ this-> QueryResult, $ row, $ field) failed .");
- Return 0;
-
- // Here shoshould have error handle
- }
-
- /** Public
- * Function: Get next record
- * Success: return a array of the record's value
- * Failed: return 0
- */
- Function nextRecord ($ result = ""){
- If ($ result! = "")
- $ This-> QueryResult = $ result;
- $ Record = @ mssql_fetch_array ($ this-> QueryResult );
- If (is_array ($ record ))
- Return $ record;
- // $ This-> halt ("Get the next record Failed: the Result $ result is invalid .");
- Return 0;
- }
-
- /** Public
- * Function: Free the Query Result
- * Success return 1
- * Failed: return 0
- */
- Function freeResult ($ result = ""){
- If ($ result! = "")
- $ This-> QueryResult = $ result;
- Return @ mssql_free_result ($ this-> QueryResult );
- }
-
- /** Public
- * Function: Set the Halt_On_Error's state
- * Success: return 1
- * Failed: return 0
- */
- Function setHaltOnError ($ state = "ignore "){
- If (! ($ State = "ignore" | $ state = "report" | $ state = "halt ")){
- $ This-> halt ("Set the Halt_On_Error Fail: There is no state value $ state ");
- Return 0;
- }
- $ This-> Halt_On_Error = $ state;
- Return 1;
- }
-
- /** Public
- * Function: Get the Halt_On_Error's state
- */
- Function getHaltOnError (){
- Return $ this-> Halt_On_Error;
- }
-
- /** Public
- * Function: Get the class's name
- */
- Function toString (){
- Return $ this-> ClassName;
- }
-
- /** Private
- * Function: Error handle
- */
- Function halt ($ msg ){
- $ This-> Error = @ mysql_error ($ this-> LinkID );
- $ This-> ErrNo = @ mysql_errno ($ this-> LinkID );
- If ($ this-> Halt_On_Error = "ignore ")
- Return;
- $ This-> makeMsg ($ msg );
- If ($ this-> Halt_On_Error = "halt ")
- Die ("Session halted ");
- }
-
- /** Private
- * Function: Make error information and print
- */
- Function makeMsg ($ msg ){
- Printf ("Database error: % s \ n", $ msg );
- Printf ("MySQL Error: % s (% s) \ n", $ this-> ErrNo, $ this-> Error );
- }
- }
|