Php Mssql操作简单封装支持存储过程


Posted in PHP onDecember 11, 2009

核心代码:

<?php
/*
 * class :Mssql
 * time :2009-12-10
 * author :Libaochang
 * version :1.0b
 * description :mssql database access class,it can execute the procedur or sql
*/
class MssqlUtil {
  var $user = null; //database user name
  var $keys = null; //database user password
  var $host = 'localhost'; //database host name/ip and port
  var $base = null; //database name
  var $link = null; //create link
  
  /** 
   * construct function init all parmeters
   * @param <type> $host database host name/ip and port
   * @param <type> $user database user name
   * @param <type> $keys database user password
   * @param <type> $base database name
   */
  function __construct($host, $user, $keys, $base) {
    $this->host = $host;
    $this->user = $user;
    $this->keys = $keys;
    $this->base = $base;
  }
  /** 
   * create the connection
   */
  function connect() {
    $this->link = mssql_connect($this->host, $this->user, $this->keys);
    if (!$this->link) {
      die('connecting failed...check the module and setting...');
    }
    $select = mssql_select_db($this->base, $this->link);
    if (!$select) {
      die('data base is not exist...,please checke it ...');
    }
  }
  /** 
   * execute the procedur width the parameter
   * @param <type> $pName procedur name
   * @param <type> $parName parameters it's like this $par=array('@a'=>'a')
   * @param <type> $sqlTyle the procedur's parameter type, it's llike this $sqlType=array(SQLVARCHAR,SQLVARCHAR); and there is not the char single quote mark(').
   * @return <type> object array
   */
  function executeProcedur($pName, $parName, $sqlTyle) {
    $this->connect();
    $stmt = mssql_init($pName, $this->link);
    if (isset($parName)) {
      $i = 0;
      foreach ($parName as $par => $value) {
        mssql_bind($stmt, $par, $value, $sqlTyle[$i]);
        ++$i;
      }
      $res = mssql_execute($stmt);
      $this->close();
      while ($row = mssql_fetch_assoc($res)) {
        $r[] = $row;
      }
      unset($i);
      mssql_free_result($res);
      mssql_free_statement($stmt);
      return $r;
    }
  }
  /** 
   * execute procedur without the parameter
   * @param <type> $pName Procedur Name
   * @return <type> object array
   */
  function executeProcedurNoPar($pName) {
    $this->connect();
    $stmt = mssql_init($pName, $this->link);
    $res = mssql_execute($stmt);
    $this->close();
    while ($row = mssql_fetch_assoc($res)) {
      $r[] = $row;
    }
    mssql_free_result($res);
    mssql_free_statement($stmt);
    return $r;
  }
  /** 
   * Get one row return Array
   * @param <type> $sql
   * @return <type> Array
   */
  function getRowArray($sql) {
    $res = $this->query($sql);
    $r = mssql_fetch_row($res);
    mssql_free_result($res);
    return $r;
  }
  /** 
   * Get one row return object
   * @param <type> $sql Sql
   * @return <type> Object
   */
  function getRowObject($sql) {
    $res = $this->query($sql);
    $r = mssql_fetch_assoc($res);
    return $r;
  }
  /** 
   * Execute one sql
   * @param <type> $sql Sql
   * @return <type> result
   */
  function query($sql) {
    $this->connect();
    $res = mssql_query($sql, $this->link);
    $this->close();
    return $res;
  }
  /** 
   * Get every row from result by Object, Return a Array with every element is Object
   * @param <type> $sql
   * @return <type> Object Array result
   */
  function getResult($sql) {
    $res = $this->query($sql);
    while ($row = mssql_fetch_assoc($res)) {
      $r[] = $row;
    }
    unset($row);
    mssql_free_result($res);
    return $r;
  }
  /** 
   * execute a sql
   * @param <type> $sql Sql
   */
  function executeSql($sql) {
    return $this->query($sql);
  }
  /** 
   * execute a sql statement
   * @param <type> $sql
   * @return <type> int $affected rows
   */
  function querySql($sql) {
    $this->connect();
    mssql_query($sql, $this->link);
    $affected = mssql_rows_affected($this->link);
    $this->close();
    return $affected;
  }
  /** 
   * close connection
   */
  function close() {
    mssql_close();
  }
}
?>

下面说下调用

function __autoload($MssqlUtil) { 
require $MssqlUtil.'.php'; 
} 
$db = new MssqlUtil($config['host'],$config['user'],$config['keys'],$config['base']);

主要说下带参数的存储过程调用

$pName 存储过程名字 
$parName 参数,参数形式很重要,是数组类型,对应关系为 
array('@a'=>'a') @a 为存储过程里面的参数,a为要传递的值 
$sqlTyle 是存储过程参数的数据类型,是数组形式,也很重要 
array(SQLCHAR,SQLVARCHAR),注意不要加单引号等,因为SQLVARCHAR是SQL的一些常量 

带参数存储过程 
$db->executeProcedur($pName,$parName,$sqlTyle); 
无参数存储过程 
$db->executeProcedurNoPar($pName);

select * from t2 where t2.id in(select max(t2.id) from t1 join t2 on t1.id = t2.pid group by t1.id);
取每个分类的最新一条数据。此处做个记录。
t1为类别表,t2为主表

PHP 相关文章推荐
如何使用PHP往windows中添加用户
Dec 06 PHP
PHP随机字符串生成代码(包括大小写字母)
Jun 24 PHP
PHP根据传入参数合并多个JS和CSS文件的简单实现
Jun 13 PHP
ThinkPHP分组下自定义标签库实例
Nov 01 PHP
你应该知道PHP浮点数知识
May 13 PHP
YII使用url组件美化管理的方法
Dec 28 PHP
PHP页面间传递值和保持值的方法
Aug 24 PHP
PHP实现微信图片上传到服务器的方法示例
Jun 29 PHP
详解php中curl返回false的解决办法
Mar 18 PHP
Thinkphp5 如何隐藏入口文件index.php(URL重写)
Oct 16 PHP
laravel 解决多库下的DB::transaction()事务失效问题
Oct 21 PHP
laravel 框架实现无限级分类的方法示例
Oct 31 PHP
php smarty模版引擎中变量操作符及使用方法
Dec 11 #PHP
phpmyadmin导入(import)文件限制的解决办法
Dec 11 #PHP
php smarty模版引擎中的缓存应用
Dec 11 #PHP
php5 图片验证码实现代码
Dec 11 #PHP
php下图片文字混合水印与缩略图实现代码
Dec 11 #PHP
一个比较简单的PHP 分页分组类
Dec 10 #PHP
PHP 采集程序中常用的函数
Dec 09 #PHP
You might like
[转帖]PHP世纪万年历
2006/12/06 PHP
浅析php与数据库代码开发规范
2013/08/08 PHP
PHP错误WARNING: SESSION_START() [FUNCTION.SESSION-START]解决方法
2014/05/04 PHP
神盾加密解密教程(二)PHP 神盾解密
2014/06/08 PHP
ThinkPHP实现动态包含文件的方法
2014/11/29 PHP
Laravel中使用自己编写类库的3种方法
2015/02/10 PHP
解析 thinkphp 框架中的部分方法
2017/05/07 PHP
PHP常用字符串函数用法实例总结
2020/06/04 PHP
jQuery 使用手册(二)
2009/09/23 Javascript
Jquery ThickBox插件使用心得(不建议使用)
2010/09/08 Javascript
jquery的ajax简单结构示例代码
2014/02/17 Javascript
Js表格万条数据瞬间加载实现代码
2014/02/20 Javascript
JavaScript获取图片真实大小代码实例
2014/09/24 Javascript
Eclipse配置Javascript开发环境图文教程
2015/01/29 Javascript
jquery mobile 移动web(5)
2015/12/20 Javascript
jQuery中text() val()和html()的区别实例详解
2016/06/28 Javascript
详解用vue编写弹出框组件
2017/07/04 Javascript
Vue自定义过滤器格式化数字三位加一逗号实现代码
2018/03/23 Javascript
小程序自定义导航栏兼容适配所有机型(附完整案例)
2020/04/26 Javascript
JavaScript实现网页tab栏效果制作
2020/11/20 Javascript
[03:40]2014DOTA2国际邀请赛 B神专访:躲箭真的很难
2014/07/13 DOTA
python开发之IDEL(Python GUI)的使用方法图文详解
2015/11/12 Python
Python数据分析之真实IP请求Pandas详解
2016/11/18 Python
python中reload(module)的用法示例详解
2017/09/15 Python
3分钟学会一个Python小技巧
2018/11/23 Python
Python提取特定时间段内数据的方法实例
2019/04/01 Python
Python高级property属性用法实例分析
2019/11/19 Python
GANT葡萄牙官方商店:拥有美国运动服传统的生活方式品牌
2018/10/18 全球购物
医学实习生自我鉴定
2013/12/12 职场文书
晚会邀请函范文
2014/01/24 职场文书
物业管理工作方案
2014/05/10 职场文书
金融与证券专业求职信
2014/06/22 职场文书
2015年世界环境日演讲稿
2015/03/18 职场文书
个人求职信格式范文
2015/03/20 职场文书
2015年节能降耗工作总结
2015/05/22 职场文书
Python中使用subprocess库创建附加进程
2021/05/11 Python