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

 

  1. <?php   
  2. /*  
  3. * class :Mssql  
  4. * time :2009-12-10  
  5. * author :Libaochang  
  6. * version :1.0b  
  7. * description :mssql database access class,it can execute the procedur or sql  
  8. */   
  9. class MssqlUtil   
  10. {   
  11. var $user = null; //database user name   
  12. var $keys = null; //database user password   
  13. var $host = 'localhost'//database host name/ip and port   
  14. var $base = null; //database name   
  15. var $link = null; //create link   
  16. /**  
  17. * construct function init all parmeters  
  18. * @param <type> $host database host name/ip and port  
  19. * @param <type> $user database user name  
  20. * @param <type> $keys database user password  
  21. * @param <type> $base database name  
  22. */   
  23. function __construct($host,$user,$keys,$base)   
  24. {   
  25. $this->host = $host;   
  26. $this->user = $user;   
  27. $this->keys = $keys;   
  28. $this->base = $base;   
  29. }   
  30. /**  
  31. * create the connection  
  32. */   
  33. function connect()   
  34. {   
  35. $this->link = mssql_connect($this->host,$this->user,$this->keys);   
  36. if(!$this->link)   
  37. {   
  38. die('connecting failed...check the module and setting...');   
  39. }   
  40. $select = mssql_select_db($this->base,$this->link);   
  41. if(!$select)   
  42. {   
  43. die('data base is not exist...,please checke it ...');   
  44. }   
  45. }   
  46. /**  
  47. * execute the procedur width the parameter  
  48. * @param <type> $pName procedur name  
  49. * @param <type> $parName parameters it's like this $par=array('@a'=>'a')  
  50. * @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(').  
  51. * @return <type> object array  
  52. */   
  53. function executeProcedur($pName,$parName,$sqlTyle)   
  54. {   
  55. $this->connect();   
  56. $stmt = mssql_init($pName,$this->link);   
  57. if(isset($parName))   
  58. {   
  59. $i = 0;   
  60. foreach($parName as $par=>$value)   
  61. {   
  62. mssql_bind($stmt,$par,$value,$sqlTyle[$i]);   
  63. ++$i;   
  64. }   
  65. $res = mssql_execute($stmt);   
  66. $this->close();   
  67. while($row=mssql_fetch_assoc($res))   
  68. {   
  69. $r[] = $row;   
  70. }   
  71. unset($i);   
  72. mssql_free_result($res);   
  73. mssql_free_statement($stmt);   
  74. return $r;   
  75. }   
  76. }   
  77. /**  
  78. * execute procedur without the parameter  
  79. * @param <type> $pName Procedur Name  
  80. * @return <type> object array  
  81. */   
  82. function executeProcedurNoPar($pName)   
  83. {   
  84. $this->connect();   
  85. $stmt = mssql_init($pName,$this->link);   
  86. $res = mssql_execute($stmt);   
  87. $this->close();   
  88. while($row=mssql_fetch_assoc($res))   
  89. {   
  90. $r[] = $row;   
  91. }   
  92. mssql_free_result($res);   
  93. mssql_free_statement($stmt);   
  94. return $r;   
  95. }   
  96. /**  
  97. * Get one row return Array  
  98. * @param <type> $sql  
  99. * @return <type> Array  
  100. */   
  101. function getRowArray($sql)   
  102. {   
  103. $res = $this->query($sql);   
  104. $r = mssql_fetch_row($res);   
  105. mssql_free_result($res);   
  106. return $r;   
  107. }   
  108. /**  
  109. * Get one row return object  
  110. * @param <type> $sql Sql  
  111. * @return <type> Object  
  112. */   
  113. function getRowObject($sql)   
  114. {   
  115. $res = $this->query($sql);   
  116. $r = mssql_fetch_assoc($res);   
  117. return $r;   
  118. }   
  119. /**  
  120. * Execute one sql  
  121. * @param <type> $sql Sql  
  122. * @return <type> result  
  123. */   
  124. function query($sql)   
  125. {   
  126. $this->connect();   
  127. $res = mssql_query($sql,$this->link);   
  128. $this->close();   
  129. return $res;   
  130. }   
  131. /**  
  132. * Get every row from result by Object, Return a Array with every element is Object  
  133. * @param <type> $sql  
  134. * @return <type> Object Array result  
  135. */   
  136. function getResult($sql)   
  137. {   
  138. $res = $this->query($sql);   
  139. while($row=mssql_fetch_assoc($res))   
  140. {   
  141. $r[] = $row;   
  142. }   
  143. unset($row);   
  144. mssql_free_result($res);   
  145. return $r;   
  146. }   
  147. /**  
  148. * execute a sql  
  149. * @param <type> $sql Sql  
  150. */   
  151. function executeSql($sql)   
  152. {   
  153. return $this->query($sql);   
  154. }   
  155. /**  
  156. * execute a sql statement  
  157. * @param <type> $sql  
  158. * @return <type> int $affected rows  
  159. */   
  160. function querySql($sql)   
  161. {   
  162. $this->connect();   
  163. mssql_query($sql,$this->link);   
  164. $affected = mssql_rows_affected($this->link);   
  165. $this->close();   
  166. return $affected;   
  167. }   
  168. /**  
  169. * close connection  
  170. */   
  171. function close()   
  172. {   
  173. mssql_close();   
  174. }   
  175. }   
  176. ?>   

 

 

调用

 

 

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

 

 

 

带参数的存储过程调用

 

 

 

  1. $pName 存储过程名字   
  2. $parName 参数,参数形式很重要,是数组类型,对应关系为   
  3. array('@a'=>'a') @a 为存储过程里面的参数,a为要传递的值   
  4. $sqlTyle 是存储过程参数的数据类型,是数组形式,也很重要   
  5. array(SQLCHAR,SQLVARCHAR),注意不要加单引号等,因为SQLVARCHAR是SQL的一些常量   
  6. 带参数存储过程   
  7. $db->executeProcedur($pName,$parName,$sqlTyle);   
  8. 无参数存储过程   
  9. $db->executeProcedurNoPar($pName); 

 

posted @ 2012-03-30 16:12  rob_2010  阅读(318)  评论(0)    收藏  举报