Many friends asked me how to use php program to connect to the general class of SQL Server database. In fact, this is not difficult. The key is to be a little careful, a little patient, and understand 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: Display page processing error information.
****************************************/
function Halt($msg) {
printf("Database error: %s
n", $msg);
printf("mssql Error: %s (%s)
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");
}
}
}
/**************************************
*Close the database, if the database connection is already open, close it
*Please use Close()
after calling Connect() and processing
****************************************/
function Close() {
if (0 != $this->Link_ID){
mssql_close();
}
}
/******************************************************
*Enter sql statements, including select, update, insert, delete