当前位置:首页 > 数据库 > Sqlserver

分享一个PHP连接sqlserver的类

大家平常见的最多的是php连接mysql的类,今天给大家分享一个php连接sql server的类。 感兴趣的朋友可以参考下。

  1. <?php

  2. class DB_Handle {
  3. var $ClassName = "DB_Handle";
  4. var $Server;
  5. var $UserName;
  6. var $Password;
  7. var $Database;
  8. var $LinkID = 0;
  9. var $QueryResult = "";
  10. var $LastInsertID = "";
  11. /* private ignore=>ignore the error and continue, halt=>report the error and halt, report=>report the error and continue */
  12. var $Halt_On_Error = "report";
  13. var $Error = "";
  14. var $ErrNo = 0;
  15. /**public
  16. * remark: This is the db_mysql_class's structure
  17. * function: Set the server,username,password,database variable.
  18. */
  19. function DB_Handle($server = "", $username = "", $password = "", $database = "") {
  20. $this->Server = $server;
  21. $this->UserName = $username;
  22. $this->Password = $password;
  23. $this->Database = $database;
  24. }
  25. /**public
  26. * function: Connect database and select database
  27. * success: retun 1
  28. * failed: return 0
  29. */
  30. function connect() {
  31. $this->LinkID = @mssql_pconnect ( $this->Server, $this->UserName, $this->Password );
  32. if (! $this->LinkID) {
  33. $this->halt ( "mssql_pconnect($this->Server,$this->UserName,$this->Password): Failed" );
  34. return 0;
  35. }
  36. if (! @mssql_select_db ( $this->Database )) {
  37. $this->halt ( "mssql_select_db($this->Database) Failed." );
  38. return 0;
  39. }
  40. return 1;
  41. }
  42. /**public
  43. * function: Check the database, if exist then select
  44. * exist: return 1
  45. * not exist: return 0
  46. */
  47. function selectDatabase() {
  48. if (@mssql_select_db ( $this->Database ))
  49. return 1;
  50. else
  51. return 0;
  52. }
  53. /**public
  54. * function: Execute SQL instruction
  55. * success: return SQL Result.
  56. * failed: return 0;
  57. */
  58. function execQuery($sql = "") {
  59. $this->connect();
  60. if ($this->LinkID == 0) {
  61. $this->halt ( "Execute SQL Failed: Have not valid database connect." );
  62. return 0;
  63. }
  64. ob_start ();
  65. $this->QueryResult = mssql_query ( $sql, $this->LinkID );
  66. $error = ob_get_contents ();
  67. ob_end_clean ();
  68. if ($error) {
  69. $this->halt ( "Execute SQL: mssql_query($sql,$this->LinkID) failed." );
  70. return 0;
  71. }
  72. $reg = "#insert into#";
  73. if (preg_match ( $reg, $sql )) {
  74. $sql = "select @@IDENTITY as id";
  75. $res = mssql_query ( $sql, $this->LinkID );
  76. $this->LastInsertID = mssql_result ( $res, 0, id );
  77. }
  78. return $this->QueryResult;
  79. }
  80. /**public
  81. * function: Get the query result's row number
  82. * success: return the row fo the Result
  83. * failed: return 0
  84. */
  85. function getTotalRowNum($result = "") {
  86. if ($result != "")
  87. $this->QueryResult = $result;
  88. $row = @mssql_num_rows ( $this->QueryResult );
  89. if ($row >= 0)
  90. return $row;
  91. $this->halt ( "Get a row of result Failed: Result $result is invalid." );
  92. return 0;
  93. }
  94. /**public
  95. * function: Get the last insert record's id
  96. * success: return id
  97. * failed: return 0
  98. */
  99. function lastInsertID() {
  100. return $this->LastInsertID;
  101. }
  102. /**public
  103. * function: Get a field's value
  104. * success: return value of the field
  105. * failed: return 0
  106. */
  107. function getField($result = "", $row = 0, $field = 0) {
  108. if ($result != "")
  109. $this->QueryResult = $result;
  110. $fieldvalue = @mssql_result ( $this->QueryResult, $row, $field );
  111. if ($fieldvalue != "")
  112. return $fieldvalue;
  113. $this->halt ( "Get field: mssql_result($this->QueryResult,$row,$field) failed." );
  114. return 0;
  115. //Here should have error handle
  116. }
  117. /**public
  118. * function: Get next record
  119. * success: return a array of the record's value
  120. * failed: return 0
  121. */
  122. function nextRecord($result = "") {
  123. if ($result != "")
  124. $this->QueryResult = $result;
  125. $record = @mssql_fetch_array ( $this->QueryResult );
  126. if (is_array ( $record ))
  127. return $record;
  128. //$this->halt("Get the next record Failed: the Result $result is invalid.");
  129. return 0;
  130. }
  131. /**public
  132. * function: Free the Query Result
  133. * success return 1
  134. * failed: return 0
  135. */
  136. function freeResult($result = "") {
  137. if ($result != "")
  138. $this->QueryResult = $result;
  139. return @mssql_free_result ( $this->QueryResult );
  140. }
  141. /**public
  142. * function: Set the Halt_On_Error's state
  143. * success: return 1
  144. * failed: return 0
  145. */
  146. function setHaltOnError($state = "ignore") {
  147. if (! ($state == "ignore" || $state == "report" || $state == "halt")) {
  148. $this->halt ( "Set the Halt_On_Error Fail: There is no state value $state" );
  149. return 0;
  150. }
  151. $this->Halt_On_Error = $state;
  152. return 1;
  153. }
  154. /**public
  155. * function: Get the Halt_On_Error's state
  156. */
  157. function getHaltOnError() {
  158. return $this->Halt_On_Error;
  159. }
  160. /**public
  161. * function: Get the class's name
  162. */
  163. function toString() {
  164. return $this->ClassName;
  165. }
  166. /**private
  167. * function: Error handle
  168. */
  169. function halt($msg) {
  170. $this->Error = @mysql_error ( $this->LinkID );
  171. $this->ErrNo = @mysql_errno ( $this->LinkID );
  172. if ($this->Halt_On_Error == "ignore")
  173. return;
  174. $this->makeMsg ( $msg );
  175. if ($this->Halt_On_Error == "halt")
  176. die ( "Session halted" );
  177. }
  178. /**private
  179. * function: Make error information and print
  180. */
  181. function makeMsg($msg) {
  182. printf ( "Database error: %sn", $msg );
  183. printf ( "MySQL Error: %s (%s)n", $this->ErrNo, $this->Error );
  184. }
  185. }

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐