Home Backend Development PHP Tutorial Share a PHP class to connect to sql server

Share a PHP class to connect to sql server

Jul 25, 2016 am 09:11 AM

The most commonly seen class is the PHP class that connects to MySQL. Today I will share with you a PHP class that connects to SQL server. Interested friends can refer to it.

  1. <p><?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. }</p>
复制代码


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles