PHP程序来连接SQLServer数据库的通用类
程序员文章站
2022-06-14 11:07:57
...
很多朋友问我如何用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("Database error: %s
n", $msg);
printf("mssql Error: %s (%s)
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
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("Database error: %s
n", $msg);
printf("mssql Error: %s (%s)
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
上一篇: PHP面向对象和PHP面向过程的优缺点评比_PHP教程
下一篇: 如何让外网访问局域网中的一台机器