Share a PHP connection to sqlserver class-php Tutorial

Source: Internet
Author: User
Share a PHP connection to sqlserver class

What we usually see most is the php connection to mysql class. today we will share with you a class for connecting php to SQL server. For more information, see.

  1. Class DB_Handle {

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.