php mssql 数据库连接类代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-11 23:30:01 浏览: 评论:0
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
- /* public: constructorwww.phpfensi.com */
- 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)];
- }//开源代码phpfensi.com
- 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.");
- }
- }
Tags: php mssql数据库连接类
- 上一篇:通用mysql数据库连接类代码
- 下一篇:mysql数据库连接类
相关文章
- ·PHP多功能图片处理类(2013-11-11)
- ·PHP 生成缩略图的类(2013-11-13)
- ·一个分页显示类(2013-11-13)
- ·分享的一个分页类(2013-11-13)
- ·简单的php分页类(2013-11-14)
- ·一个功能比较高的分页类(for PHP5.x)(2013-11-28)
- ·phpword中文字符乱码解决办法(2013-12-05)
- ·一个比较完善的购物车类(2013-12-08)
- ·php面象对象数据库操作类(2013-12-09)
- ·PHP顶层类(2013-12-10)
- ·PHP静态文件生成类(2013-12-10)
- ·PHP分页类(2013-12-10)
- ·非常简单的日历类(2013-12-11)
- ·php数字分页类的代码(2013-12-23)
- ·PHP货币换算程序代码(2013-12-27)
- ·实现多文件上传php类(2014-01-03)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)