很多朋友问我如何用php程序来连接SQL Server数据库的通用类,其实这个并不是很难的,关键是一点点仔细,一点点耐心,用激情去理解
<?
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;
/**************************************
*打印错误方法:显示页面处理的错误信息。
****************************************/
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.");
}
/**************************************
*连接数据库,并且选择默认的数据库
**************************************/
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");
}
}
}
/****************************************
*关闭数据库,如果数据库连接已经打开则关闭它
*请在调用Connect()并处理后使用Close()
****************************************/
function Close() {
if (0 != $this->Link_ID){
mssql_close();
}
}
/*************************************************
*输入sql语句,有select,update,insert,delete
*包括存储过程也可以通过这个方法来调用。
*************************************************/
function Query($Query_String) {
$this->Connect();
$this->Query_ID = mssql_query($Query_String);
$this->Row = 0;
if (!$this->Query_ID) {
$msg=mssql_get_last_message();
if($msg==null || $msg=="")
{
$this->AffNum=1;
return 1;
}
if(strtolower(substr($Query_String,0,6))!="select"){
$this->AffNum=1;
return 1;
}
$this->Errno = 1;
$this->Error = "General Error (The mssql interface cannot return detailed error messages)(".$msg.").";
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
/*******************************************************
*把查询数据库的指针移到下一条记录
*******************************************************/
function NextRecord() {
$this->Record = array();
mssql_next_result($this->Query_ID);
&