Many of my friends asked me how to use a php program to connect to a common class of SQL Server database. In fact, this is not very difficult. The key point is to be careful, patient, and understanding with passion.
<?
Class DB {
Var $ Host = "192.168.0.199"; // Hostname of our MySQL server
Var $ Database = "test"; // Logical database name on that server
Var $ User = "user"; // Database user
Var $ Password = "password"; // Database user's password
Var $ Link_ID = 0; // Result of mssql_connect ()
Var $ Query_ID = 0; // Result of most recent mssql_query ()
Var $ Row = 0; // Current row number
Var $ Errno = 0; // Error state of query
Var $ Error = "";
Var $ AffNum = 0;
/**************************************
* Print error method: displays the error information processed on the page.
****************************************/
Function Halt ($ msg ){
Printf ("</td> </tr> </table> <B> Database error: </B> % s <br> n", $ msg );
Printf ("<B> mssql Error </B>: % s (% s) <br> n ",
$ This-> Errno,
$ This-> Error );
Die ("Session halted .");
}
/**************************************
* Connect to the database and select the default database
**************************************/
Function Connect (){
If (0 = $ this-> Link_ID ){
$ This-> Link_ID = mssql_connect ($ this-> Host, $ this-> User, $ this-> Password) or die ("Couldn't connect to SQL Server on
$ Servername ");
Database, $ this-> Link_ID "> $ db = @ mssql_select_db ($ this-> Database, $ this-> Link_ID );
If (! $ This-> Link_ID ){
$ This-> Halt ("Link-ID = false, mssql_connect failed ");
}
}
}
/*************************************** *
* Shut down the database. If the database connection is already enabled, close it.
* Use Close () after Connect () is called and processed ()
****************************************/
Function Close (){
If (0! = $ This-> Link_ID ){
Mssql_close ();
}
}
/*************************************** **********
* The input SQL statements include select, update, insert, and delete.