Recently rewritten the database coupling class in PHP, add the features of the paging.
/ **? * Filename: db_mssql.class.php? * Function: Database connection class? * Date: ?? 2004-07-06 ?? * author: ?? Kelphen? * Modify: Modify the paging function, paid Settings and paging conditions Hide? * / Class DB_MSSQL {/ **? * Database server name? *? * @Var string? * @Access private? * /? Var $ host ???? = ""; / **? * Database name? *? * @Var string? * @Access private? * /? Var $ database = ""; / **? * Database allowed users? *? * @Var string? * @Access PRIVATE? * /? var $ user ???? = ""; / **? * Database access password? *? * @var string? * @access private? * /? var $ password = ""; / ** ? * Connect the connection number returned by the database, return the connection to the database connection code, failed to return false value? *? * @Var string? * @Access private? * /? Var $ link_id? = 0; / **? * The status value returned after executing the SQL statement, returns True? *? * @Var int? * @Access private? * /? Var $ query_id = 0; / **? * Store the record set array? *? * @ VAR Array? * @access private? * /? var $ record ?? = array (); / **? * Record pointer? *? * @var int? * @access private? * /? var $ row ??? ?? = 0; / **? * Error number? *? * @Var int? * @Access private? * /? Var $ errno ??? = 0; / **? * Error message? *? * @Var String? * @access private? * /? var $ error ??? = ""; / **? * Release flag of database connection resources, if $ auto_free = 1 automatically releases database resources? *? * @var int? * @access private? * /? var $ auto_free = 1; / **? * Database connection status, if $ P Connect = 1 Continuous connection database? *? * @Var int? * @Access private? * /? Var $ PCONNECT? = 0; / **? * Constructor. There are 5 parameters, where $ query can be omitted.? *? * Usage: $ dB = & new db_msql ("192.168.0.1", "Hello", "Kelphen", "111111");? *? * @Param $ HOST Database server name? * @Param $ database database name? * @Param $ user name? * @Param $ password database password? * @Param $ query SQL statement? * @access public? * @Return void? *? * /? Function DB_MSSQL ($ Host, $ Database, $ User $ Password, $ query = "") {? $ This-> host = $ host; ?? $ this-> database = $ database;
$ this-> user = $ user; ?? $ this-> password = $ password;?} / **? * Database connection function? *? * @access private? * @Return void? * /? Function Connect () {?? IF (0 == $ this-> link_id) {??? if (! $ this-> pconnect) {???? $ this-> link_id = mssql_connect ($ this-> Host, $ THIS -> User, $ this-> password); ???} else {???? $ this-> link_id = mssql_pconnect ($ this-> Host, $ this-> user, $ this-> password); ?? ?} ??? if (! $ this-> link_id) {???? $ this-> Halt ("Database Error", "Connect ($ this-> Host, $ this-> user, / $ password) failed "???} else {???? mssql_select_db ($ this-> Database, $ this-> link_id); ???} ??}?} / **? * Release the resource function, release the Query function The resource occupied after execution? *? * @Access private? * @RETURN VOID? * /? FUNCTION FREE_RESULT () {?? mssql_free_result ($ this-> query_id); ?? $ this-> query_id = 0 ;?} / **? * Database query function, execute the SQL statement, successfully returns the query record number, otherwise return false? *? * Usage: $ db-> query ($ sql);? * @Param $ query_string? * @Access public? * @RTurn int? * /? Function Query ($ query_string) {?? if ($ query_string == "") {??? return 0; ??} ?? if (! $ this-> link_id) {?? $ this-> connect (); ??} ?? $ this-> query_id = mssql_query ($ query_string, $ this-> link_id); ?? $ this-> row = 0; ?? // $ this-> row = $ this-> Seek ($ POS); ?? if (! $ this-> query_id) {??? $ this-> errno = 1; ??? $ this-> error = "General Error (SQL Server database cannot return detailed error message). "; ?? $ this-> Halt (" debugging information "," invalid SQL statement: "$ query_string); ??} ?? return $ this- > Query_id;?} / **? * Get the current record data, the record set pointer move down? *? * Usage: $ dB-> next_record ();? * @Access public? * @RTurn int? * /? FUNCTION Next_record () {?? f ($ this-> record = mssql_fetch_row ($ this-> query_id)) {??? $ count = mssql_num_fields ($ this-> query_id); ??? for ($ I = 0; $ I <$ count; $ i
) {???? $ fieldinfo = mssql_fetch_field ($ this-> query_id, $ i); ???? $ this-> record [strtolower ($ infinfo-> name)] = $ this-> record [$ I] ; ???} ??? $ this-> row = 1; ??? $ stat = 1; ??} else {??? IF ($ this-> auto_free) {???? $ this-> Free_Result (); ???} ??? $ stat = 0; ??} ?? Return $ stat;?} / **? * Mobile pointer to the specified row? *? * @Param $ POS? * @access public * @RTurn int? * /? Function seek ($ POS) {?? mssql_data_seek ($ this-> query_id, $ pOS); ?? IF ($ pOS! = "") {??? $ this-> ROW = $ POS; ??} Else {??? $ this-> row = 0; ??}?} / **? * Query the information of the database field? *? * @Param $ table? * @access public? * @RTURN Array? * /? Function metadata ($ TABLE) {? $ count = 0; ?? $ ID ??? = 0; ?? $? = array ();
$ this-> connect (); ?? $ ID = MSSQL_QUERY ("SELECT * from $ TABLE", $ this-> link_id); ?? IF (! $ ID) {??? $ this-> errno = 1; ??? $ this-> error = "General Error (SQL Server Database cannot return detailed error message)."; ??? $ this-> Halt ("Database Error", "Metadata Query Failed."); ??}? $ count = mssql_num_fields ($ ID); ?? for ($ I = 0; $ I <$ count; $ i ) {??? $ info = mssql_fetch_field ($ ID, $ I);?? $ R [$ I] ["Table"] = $ table; ??? $? []】 [Name "]? = $ info-> name; ??? $ res [$ I] [" LEN "] ?? = $ info-> max_length; ??? $ res [$ i] [" flags "] = $ info-> numeric; ??} ?? Return $ RES;?} / **? * acquisition insertion The last record of the primary key value? *? * @Access public? * @RETURN STRING? * /? Function last_id () {?? $ r @@ identity as rows ", $ this-> link_id ); ?? IF ($ rROWS) {??? Return MSSQL_RESULT ($ RSROWS, 0, "ROWS"); ??}?} / **? * acquire the total number of specified record sets? *? * @access public? * @RTurn int? * /? function num_rows () {?? return mssql_num_rows ($ this-> query_id);?} / **? * acquire the total number of specified record sets? *? * @access public? * @return Int ? * /? Function nf () {?? Return $ this-> num_rows ();?} / **? * Print the total number of specified record set? *? * @access public? * @return int? * /? FUNCTION Np () {?? print $ this-> Num_r OWS ();?} / **? * Number of specified record set fields? *? * @access public? * @return int? * /? Function Num_fields () {?? Return MSSQL_NUM_FIELDS ($ this-> query_id) } / **? * Note the data of the corresponding field? *? * @Param $ field_name field name? * @Access public? * @Return string? * /? Function F ($ FIELD_NAME) {?? foreach ($ FOREACH) This-> Record as $ key => $ value) {??? if ($ key == starter ($ field_name)) {???? return $ this-> record [strtolower ($ field_name)]; ??? } ??} ?? $ this-> errno = 3; ?? $ this-> error = "";? $ this-> halt ("
Database Error, "Column Name". $ Field_name. "Invalid");?} / **? * Print Output field content? *? * @Param $ field_name field name? * @Access public? * @Return Void? * /? Function P ($ FIELD_NAME) {?? print $ this-> f ($ field_name);?} / **? * Setup error message? *? * @Param $ error_no error number? * @Param $ error_title error title ? * @access public? * @RETURN VOID? * /? FUNCTION SET_ERROR ($ error_no, $ error_title) {?? $ this-> errno = $ error_no; ?? $ this-> error = $ error_title;?} / * *? * Print the error message and force the system to stop running? *? * @Param $ TYPE Error type? * @Param $ msg error message? * @Access public? * @RETURN VOID? * /? Function Halt ($ TYPE, $ TYPE, $ TYPE, $ TYPE, MSG) {?? Printf ("$ type:% s / n", $ msg); ?? Printf ("System Tip Error:% S (% s) / N", $ this-> Errno, $ this-> Error); ?? DIE ("Operation exception stop.");?} / **? * Paging navigation, call this method directly, hide the setup settings.? *? * @Access public? * @Void start? * /? function getPagenavigation () {?? // Initialize the paging parameters, do paging preparation? $ prevaGe = 0;? $ nextpage = 0;? $ strpage = ""; ?? // Home and previous one Page link ?? IF ($ this-> sumrows> 1 && $ this-> page> 1) {??? $ prevpage = $ this-> page-1; ??? $ strpage. = ""; ?? $ STRPAGE. = "DEALPAGEPARA (" Page = ")."> Home "; $ strpage. =" DEALPAGEPARA ("Page ="). "> Previous"; ??? $ strpage. = " "; ??} else {??? $ strpage. =" Home "; ??? $ strpage. =" Previous "; ??} ?? // Next page and the last page link ?? IF ($ this-> page> = 1 && $ this-> page <$ this-> maxpage) {??? $ nextpage = $ This-> page 1; ??? $ strpage. = ""; ??? $ strpage. = "DEALPAGEPAR (" Page = ")."> Next page "; $ strpage. =" Maxpage & " $ this-> DealPagePara ("page ="). "> Last page"; $ strpage. = ""; ??} else {??? $ strpage. = "Next";?? $ Strpage. = "Last page"; ??}? $ Strpage. = "
$ This-> page, a total of $ this-> maxpage page, a total of $ this-> sumrows record "; ?? Return $ strpage;?} / **? * Processing paging condition parameters, including GET and POST? * ? * @Param $ para_get usually Page string? * @access private? * @void string? * /? Function DEALPAGEPAR ($ para_get) {?? if (sizeof ($ _ post)> 0) {??? foreach $ _POST AS $ Key => $ value) {???? // loop analysis of the key value of the _POST variable ???? if (! Empty ($ value)) {????? $ para_post. = $ Key. "=". Urlencode ($ value). "&"; ????} ???} ??} ?? Return EREG_REPLACE ($ Para_Get. "[0-9] & *", "" $ GLOBALS ["Query_String"]. $ Para_post);?} / **? * The biggest page? *? * @Var int? * @Access private? * /? Var $ maxpage = 0; / **? * Record total ? *? * @var int? * @access private? * /? var $ suMRows = 0; / **? * Per page record number? *? * @var int? * @access private? * /? var $ point_size = 0; / **? * Current page? *? * @Var int? * @Access private? * /? Var $ PAGE = 0; / **? * Initialize the paging parameters, divide the data set according to the size of the page Block, make paging preparation? *? * @Param ?? $ Page_Size? The number of records per page ?? * @Param ?? $ page ?? Current page, do not deliver the actual argument? * @Access ?? private? * @return ?? Boolean? * /? Function setRecorderPointer ($ page_size, $ page) {? $ this-> Page_size = $ page_size; ?? $ this-> Page = page; ?? // Record total number?? $ this-> Sumrows = $ this-> num_rows (); ?? IF ($ this-> sumrows! = 0) {/ / If the total number of records is not 0 ??? IF ($ this-> Page == 0 or is_null ($ this-> page)) {// All illegal pages That is set to 1 ???? $ this-> Page = 1; ???} ??? // get recorded total page number ??? $ this-> maxpage = (int) CEIL ($ this-> sumrows / $ this-> Page_size; ??? ife ((int) $ this-> page> $ this-> maxpage) {// If the current page is greater than the total number of pages ??? $ this-> Page = $ this- > Maxpage; ???} ??? // Mobile record set pointer ??? $ this-> seek (($ this-> Page-1) * $ this-> Page_Size); ??} else {// Record Total is 0, return false ??? $ this-> Page = 0; ??? $ this-> maxpage = 0; ??? RETURN 0; ??}?}}?>