Open Database Connection (ODBC) has become an industrial standard for communicating with databases. PHP also provides standard interfaces so that PHP can call databases such as Access and SQL SERVER. The related functions are:
(1) integer odbc_connect (string dsn, string user, string password)
Connect to the source name of an ODBC database.
(2) integer odbc_exec (integer connection, string query) or odbc_do (integer connection, string query)
Execute a query on a connection.
(3) boolean odbc_fetch_row (integer result, integer row)
Obtains a row of data from a result set. The Row parameter is optional. If it is empty, the next valid Row is returned. If no other rows exist in the result set, false is returned.
(4) boolean odbc_close (integer connection)
Close a database connection. If a transaction is opened on the connection, an error is returned and the connection is not closed.
Finally, let's look at a paging example:
<?
// Set the number of entries displayed on each page
$ Show_num = 10;
$ Spages = $ pages; // avoid changing $ pages later
// Define the connection
$ Dsn = "localhost ";
$ User = "sa ";
$ Password = "";
// Calculate the total number of records
$ Rs_num = "select count (*) as id from bbs where zu = '0' and lei = '". $ lei ."'";
$ Conn_id = odbc_connect ($ dsn, $ user, $ password );
$ Rnum = odbc_exec ($ conn_id, $ rs_num );
While (odbc_fetch_row ($ rnum )){
$ Total_rs = odbc_result ($ rnum, "id"); // Add the total number of records to the $ total_rs variable.
}
// Calculate the number of page-related items
$ Nnn = $ total_rs/$ show_num; // calculate the total number of pages
$ Hnnn = intval ($ nnn); // round the total number of pages
$ Cnnnn = $ nnn-$ hnnn;
// Calculate the total number of pages required
Switch ($ cnnn ){
Case "0 ":
$ Hnnn ++;
$ Nnn = $ hnnn; // total number of pages
Break;
Default:
$ Nnn = $ hnnn; // total number of pages
Break;
};
If ($ nnn = 0) $ nnn ++;
// Calculate the page to change the required conditions
$ Fore = $ pages;
$ Next = $ pages;
$ Fore-= 1;
$ Next + = 1;
If ($ fore> 0 ){
Echo "<a> homepage </a> ";
Echo "<a> previous page </a> ";
};
If ($ pages <$ nnn ){
Echo "<a> back page </a> ";
Echo "<a> last page </a> ";
};
Echo "Total". $ nnn. "Page ";
$ Query_string = "SELECT * FROM table where condition order by you wanted order ";
$ Cur = odbc_exec ($ conn_id, $ query_string );
// Get the top of the loop
$ Cnum = ($ pages-1) * $ show_num; // calculates the cursor position of the current record.
// Empty loop to the display record cursor
If ($ cnum! = 0 ){
For ($ I = 0; $ I <= $ cnum; odbc_fetch_row ($ cur) {$ I ++ ;};
};
$ I = 1;
// Display records
While (odbc_fetch_row ($ cur )){
Echo;
If ($ I = $ show_num) {// jump out of the program when the page is not reached
Break;
};
$ I ++;
};
// Close the connection
Odbc_close ($ conn_id );
?>