PHP クラスを共有して SQL サーバーに接続する

WBOY
リリース: 2016-07-25 09:11:45
オリジナル
1002 人が閲覧しました

最も一般的に見られるクラスは、MySQL に接続する PHP クラスです。今日は、SQL サーバーに接続する PHP クラスを紹介します。 興味のある友達は参考にしてみてください。

  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. /* privateignore=>エラーを無視して続行、halt=>エラーを報告して停止、report=>報告エラーが発生し続行 */
  11. var $Halt_On_Error = "レポート";
  12. var $Error = "";
  13. var $ErrNo = 0;
  14. /**public
  15. * 注釈: これは db_mysql_class の構造です
  16. * 関数: サーバー、ユーザー名、パスワード、データベース変数を設定します。*/
  17. function DB_Handle($server = "", $username = "", $password = "", $database = "") {
  18. $this->サーバー = $server;
  19. $this->ユーザー名 = $username;
  20. $this->パスワード = $password;
  21. $this->Database = $database;
  22. }
  23. /**public
  24. * 関数: データベースに接続し、データベースを選択します
  25. * 成功: 1 を返します
  26. * 失敗: 0 を返します
  27. */
  28. function connect() {
  29. $this->LinkID = @mssql_pconnect ( $this->Server, $this-> ;ユーザー名, $this->パスワード );
  30. if (! $this->リンクID) {
  31. $this->halt ( "mssql_pconnect($this->サーバー,$this->ユーザー名,$this ->パスワード): 失敗しました" );
  32. return 0;
  33. }
  34. if (! @mssql_select_db ( $this->Database )) {
  35. $this->halt ( "mssql_select_db($this->Database) に失敗しました。" );
  36. return 0;
  37. }
  38. return 1;
  39. }
  40. /* *public
  41. * 関数: データベースをチェックし、存在する場合は select
  42. * 存在する: 1 を返す
  43. * 存在しない: 0 を返す
  44. */
  45. function selectDatabase() {
  46. if (@mssql_select_db ( $this->Database ))
  47. return 1;
  48. else
  49. return 0;
  50. }
  51. /**public
  52. * 関数: SQL 命令を実行します
  53. * 成功: SQL 結果を返します。
  54. * 失敗: 0 を返します。
  55. */
  56. function execQuery ($sql = "") {
  57. $this->connect();
  58. if ($this->LinkID == 0) {
  59. $this->halt ( "SQL の実行に失敗しました: 有効なデータベース接続がありません." );
  60. return 0;
  61. }
  62. ob_start ();
  63. $this->QueryResult = mssql_query ( $sql, $this->LinkID );
  64. $error = ob_get_contents ();
  65. ob_end_clean ();
  66. if ($error) {
  67. $this->halt ( "SQL の実行: mssql_query($sql,$this->LinkID) が失敗しました。" );
  68. return 0;
  69. }
  70. $reg = "#insert into# ";
  71. if (preg_match ( $reg, $sql )) {
  72. $sql = "ID として @@IDENTITY を選択";
  73. $res = mssql_query ( $sql, $this->LinkID );
  74. $this-> ;LastInsertID = mssql_result ( $res, 0, id );
  75. }
  76. return $this->QueryResult;
  77. }
  78. /**public
  79. * 関数: クエリ結果の行番号を取得します
  80. * 成功: 結果の行を返します
  81. * 失敗: 0 を返します
  82. */
  83. function getTotalRowNum($result = "") {
  84. if ( $result != "")
  85. $this->QueryResult = $result;
  86. $row = @mssql_num_rows ( $this->QueryResult );
  87. if ($row >= 0)
  88. return $row;
  89. $ this->halt ( "結果の行の取得に失敗しました: 結果 $result が無効です。" );
  90. return 0;
  91. }
  92. /**public
  93. * 関数: 最後の挿入レコードの ID を取得します
  94. * 成功: ID を返します
  95. * 失敗: 0 を返します
  96. */
  97. function lastInsertID() {
  98. return $this- >LastInsertID;
  99. }
  100. /**public
  101. * 関数: フィールドの値を取得します
  102. * 成功: フィールドの値を返します
  103. * 失敗: 0 を返します
  104. */
  105. function getField($result = "", $row = 0, $field = 0) {
  106. if ($result != "")
  107. $this ->QueryResult = $result;
  108. $fieldvalue = @mssql_result ( $this->QueryResult, $row, $field );
  109. if ($fieldvalue != "")
  110. return $fieldvalue;
  111. $this-> halt ( "フィールドの取得: mssql_result($this->QueryResult,$row,$field) が失敗しました。" );
  112. return 0;
  113. //ここにはエラー ハンドルがあるはずです
  114. }
  115. /**public
  116. * 関数: 次のレコードを取得します
  117. * 成功: レコードの値の配列を返します
  118. * 失敗: 0 を返します
  119. */
  120. function nextRecord($result = "") {
  121. if ($result != "")
  122. $this->QueryResult = $result;
  123. $record = @mssql_fetch_array ( $ this->QueryResult );
  124. if (is_array ( $record ))
  125. return $record;
  126. //$this->halt("次のレコードの取得に失敗しました: 結果 $result が無効です。");
  127. return 0;
  128. }
  129. /**public
  130. * 関数: クエリ結果を解放します
  131. * 成功した場合は 1 を返します
  132. * 失敗した場合は 0 を返します
  133. */
  134. function freeResult($result = "") {
  135. if ($result != "")
  136. $this->QueryResult = $result;
  137. return @mssql_free_result ( $this->QueryResult );
  138. }
  139. /**public
  140. * 関数: Halt_On_Error の状態を設定します
  141. * 成功: 1 を返す
  142. * 失敗: 0 を返す
  143. */
  144. function setHaltOnError($state = "ignore") {
  145. if (! ($state == "ignore" || $state = = "レポート" || $state == "停止")) {
  146. $this->halt ( "Halt_On_Error 失敗の設定: 状態値 $state がありません" );
  147. return 0;
  148. }
  149. $this- >Halt_On_Error = $state;
  150. return 1;
  151. }
  152. /**public
  153. * 関数: Halt_On_Error の状態を取得します
  154. */
  155. function getHaltOnError() {
  156. return $this->Halt_On_Error;
  157. }
  158. /**public
  159. * 関数: クラスの名前を取得します
  160. */
  161. function toString() {
  162. return $this->ClassName;
  163. }
  164. /**private
  165. * 関数: エラーハンドル
  166. */
  167. function halt($msg) {
  168. $this->Error = @mysql_error ( $this-> ;LinkID );
  169. $this->ErrNo = @mysql_errno ( $this->LinkID );
  170. if ($this->Halt_On_Error == "ignore")
  171. return;
  172. $this->makeMsg ( $ msg );
  173. if ($this->Halt_On_Error == "停止")
  174. die ( "セッション停止" );
  175. }
  176. /**private
  177. * 機能: エラー情報を作成して印刷
  178. */
  179. function makeMsg($msg) {
  180. printf ( "データベース エラー: %sn", $msg );
  181. printf ( "MySQL エラー: %s (%s)n", $this->ErrNo, $this->Error );
  182. }
  183. }
复制發


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!