sqlsrv.class.php A simple and convenient php sqlsrv class
2013-11-25 17:29
441 查看
sqlsrv.class.php
<?php /** * @desc A simple and convenient php sqlsrv class * @author Yaseng WwW.Yaseng.Me [Yaseng@UAUC.NET] * @link http://yaseng.me/sqlsrv-class.html */ class sqlsrv{ var $error_log = array(); var $sql_log = array(); var $query_id; var $num_rows; var $conn; //connection function sqlsrv($server, $user, $pass, $dbname) { $this->conn = @sqlsrv_connect($server, array('UID' => $user ,'PWD'=> $pass, 'Database' => $dbname)); if($this->conn === false) { $this->error_log[] = sqlsrv_errors(); die(); } } //query source function query($sql){ $stmt = sqlsrv_query($this->conn, $sql); $this->sql_log[] = $sql; if($stmt === false) { $this->error_log[] = sqlsrv_errors(); } else { $this->query_id = $stmt; $this->num_rows = $this->affectedRows(); } } //fetch data function fetch_all($sql) { $this->query($sql); $data = array(); while($row = @sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC)) { $data[] = $row; } return $data; } // $DB->count(select * from users) function fetch_one($sql){ $this->query($sql); return sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC); } // $DB->count(select count(*) from users) function count($sql){ $count=$this->fetch_one($sql); return $count[""]; } function affectedRows() { return ($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false; } } ?>
test.php
<?php include "sqlsrv.class.php"; $sql=new sqlsrv("192.168.1.109","sa","xxx","test"); $strsql="select @@version"; echo "<pre><h2>sqlsrv test</h2>"; echo "<h3> rows:</h3>"; var_dump($sql->count($strsql)); echo "<h3> sql:</h3>"; print_r($sql->sql_log); echo "<h3> errors:</h3>"; print_r($sql->error_log); echo "<h3> sqlsrv:</h3>"; print_r($sql); ?>
demo
相关文章推荐
- A simple HTTP PHP class to crawl a URL for internal and external URLs
- sqlsrv.class.php 一个 php 连接 sql server的类
- PHP与UML类图: PHP and UML Class Diagrams
- How does Android, PHP, SQL, JSON, and Remote Databases work together?
- 【php配置】用php5.6.15连接sqlserver2012加入驱动后看不到sqlsrv扩展
- php连接sqlserver,官方扩展pdo模块sqlsrv
- wampserver增加php对sqlserver的支持,增加sqlsrv和pdo_sqlsrv扩展
- php a simple smtp class
- Java Class 介绍 canonical name, simple name and class name
- (win7)IIS7下处理sqlsrv not showing up in my phpinfo
- 系统不支持:sqlsrv windows 2008 r2 下面搭建 iis+sql server +php5.5 环境遇见的一些问题
- 织梦dedesql.class.php文件漏洞解决方法
- Simple PHP encrypt and decrypt
- 解决:Fatal error: Call to undefined function parcv() in include\dedesql.class.php on line 489
- a simple search with AngularJS and PHP
- 【php配置】用php5.6.5连接sqlserver2008加入驱动后看不到sqlsrv扩展
- A Simple Log Writer And Log Searching Class
- dedecms的dedesql.class.php on line 489错误的解决方法
- [转]PHP与UML类图: PHP and UML Class Diagrams
- XML class for processing and building simple XML documents