PHP connection of MSSQL Server

zhaozj2021-02-16  50

/ ************************************************** *************** Bai cabbage core favorite PHP connection MSSQL Server class

Due to work, you need to operate the SQL Server database. Based on the previously used MySQL database operation class, it is possible to perform the PHP class for SQL Server. You can perform the connection database, execute the SQL statement, query data, and get the last insertion. Operation ID number and other functions!

*********************************************************** **************** /

Class db_handle {

Var $ classname = "db_handle"; var $ server; var $ username; var $ password; var $ dataword; 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 = $ usrname; $ this-> password = $ password; $ this-> database = $ database;} / ** public * function: connect Database and SELECT: Connect Database and SELECT Database * Success: Retun 1 * failed: Return 0 * / function connect () {$ this-> linkid = @msql_pconnect ($ this-> Server, $ this-> username, $ this-> password); if (! $ This-> linkid) {$ this-> Halt ("MSSQL_PCONNECT ($ this-> Server, $ this-> username, $ this-> password): failed"); Return 0;} i f (Database ">! @ mssql_select_db ($ this-> Database)) {$ this-> Halt (" MSSQL_SELECT_DB ($ this-> Database) failed. "); return 0;} returnit 1;} / ** public * Function: Check the Database, if exist1

/ ** public * function: Execute SQL Instruction * Success: Return SQL Result. * Failed: Return 0; * / Function ExecQuery ($ SQL = ") {IF ($ this-> linkid == 0) {$ this- > Halt ("Execute SQL Failed: Hava Not Valid Database Connect."); Return 0;} OB_Start (); $ this-> queryResult = MSSQL_QUERY ($ SQL, $ THIS-> LinkID); $ error = ob_contents (); Ob_end_clean (); if ($ error) {$ this-> Halt ("EXECUTE SQL: MSSQL_QUERY ($ SQL, $ THIS-> Linkid) Failed."); Return 0;} $ reg = "#insert INTO #"; IF (preg_match ($ reg, $ s)) {$ 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 = $ results; $ row = @msql_num_rows ($ this-> queryResult); if ($ row> = 0) Return $ row; $ this-> Halt ("Get A Row of Result Failed: Result $ Res10; Return 0;} / ** public * function: get the last insert record's id * success: returnid * failed: return 0 * / function lastinsertid () {return $ this-> lastinsertID; } / ** public * function: get a field's value * success: returnified = 0 * / function getfield ($ results = ", $ row = 0, $ field = 0) {IF ($ Result! = "" "$ this-> queryResult = $ results; $ fieldvalue = @msql_result ($ this-> queryResult, $ row, $ field); if ($ FIELDVALUE! =" "

Return $ FIELDVALUE; $ THIS-> HALT ("Get Field: MSSQL_RESULT ($ 10> QueryResult, $ ROW, $ FIELD) FAILED."); return 0; // Here Should 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 = @msql_fetch_Array ($ this-> queryResult); if (is_Array ($ Record) Return $ Record; // $ this-> Halt ("Get The next record filed: The result $ result is invalid."); Return 0;} / ** public * function: free the query result * Success Return 1 * failed: return 0 * / function freresult ($ results = ") {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-> lineid); 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);}}?>

转载请注明原文地址:https://www.9cbs.com/read-21589.html

New Post(0)