php的mssql数据库连接类实例
2015-01-24信息快讯网
这篇文章主要介绍了php的mssql数据库连接类,以一个类实例的形式演示了PHP实现针对mssql数据库的各种常用操作方法,包括对数据库的连接与增删改查等操作,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了php的mssql数据库连接类实例代码,分享给大家供大家参考。
具体实现代码如下:
class DB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row = 0;
var $Errno = 0;
var $Error = "";
var $Auto_Free = 0; ## set this to 1 to automatically free results
function DB_Sql($query = "") {
$this->query($query);
}
function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mssql_connect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID)
$this->halt("Link-ID == false, mssql_pconnect failed");
else
@mssql_select_db($this->Database, $this->Link_ID);
}
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
if (!$this->Link_ID)
$this->connect();
# printf("<br>Debug: query = %s<br> ", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0;
if (!$this->Query_ID) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
function next_record() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>]
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
}
$this->Row += 1;
$stat = 1;
} else {
if ($this->Auto_Free) {
$this->free_result();
}
$stat = 0;
}
return $stat;
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos);
$this->Row = $pos;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) {
$info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table;
$res[$i]["name"] = $info["name"];
$res[$i]["len"] = $info["max_length"];
$res[$i]["flags"] = $info["numeric"];
}
$this->free_result();
return $res;
}
function affected_rows() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
function num_rows() {
return mssql_num_rows($this->Query_ID);
}
function num_fields() {
return mssql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Field_Name) {
return $this->Record[strtolower($Field_Name)];
}
function p($Field_Name) {
print $this->f($Field_Name);
}
function halt($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br> ", $msg);
printf("<b>MSSQL Error</b>: %s (%s)<br> ",
$this->Errno,
$this->Error);
die("Session halted.");
}
}希望本文所述对大家的PHP程序设计有所帮助。
php+mysql实现无限分类实例详解
php在linux下检测mysql同步状态的方法
php+mysql删除指定编号员工信息的方法
php导入excel文件到mysql数据库的方法
php+mysql查询优化简单实例
php正则匹配html中带class的div并选取其中内容的方法
php的sso单点登录实现方法
php读取mssql的ntext字段返回值为空的解决方法
php查询mssql出现乱码的解决方法
dedecms集成财付通支付接口
VPS中使用LNMP安装WordPress教程
PHP把MSSQL数据导入到MYSQL的方法
php5.3不能连接mssql数据库的解决方法
PHP连接MSSQL时nvarchar字段长度被截断为255的解决方法
PHP使用get_headers函数判断远程文件是否存在的方法
smarty中post用法实例
PHP中mysql_field_type()函数用法
thinkphp区间查询、统计查询与SQL直接查询实例分析
PHP自定session保存路径及删除、注销与写入的方法
php中addslashes函数与sql防注入
php判断类是否存在函数class_exists用法分析
dedecms中使用php语句指南
PHP中的Streams详细介绍
php实现的css文件背景图片下载器代码
php实现压缩多个CSS与JS文件的方法
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
php中常见的sql攻击正则表达式汇总
仿dedecms下拉分页样式修改的thinkphp分页类实例
Linux下安装PHP MSSQL扩展教程
PHP连接MSSQL2008/2005数据库(SQLSRV)配置实例