Object-oriented PHP operation MSSQL class

Source: Internet
Author: User
Tags mssql
  1. /*
  2. Untested
  3. */
  4. Class Database_mssql {
  5. var $database = NULL;
  6. var $sqls = NULL;
  7. var $host = NULL;
  8. var $username = NULL;
  9. var $password = NULL;
  10. var $databaseName = NULL;
  11. var $link = NULL;
  12. var $queries = NULL;
  13. var $errors = NULL;
  14. function Database_mssql ($host, $username, $password, $database) {
  15. $this->host = $host;
  16. $this->username = SHA1 ($username);
  17. $this->password = SHA1 ($password);
  18. $this->database = $database;
  19. $this->link = "";
  20. $this->queries = Array ();
  21. $this->errors = Array ();
  22. $this->sqls = Array ();
  23. $this->link = Mssql_connect ($this->host, $username, $password);
  24. mssql_select_db ($this->database, $this->link);
  25. }
  26. function Justquery ($sql) {
  27. $this->queries[] = $sql;
  28. Return Mssql_query ($sql, $this->link);
  29. }
  30. function Loadresult ($sql) {
  31. if (! ( $cur = $this->justquery ($sql))) {
  32. return null;
  33. }
  34. $ret = null;
  35. if ($row = Mssql_fetch_row ($cur)) {
  36. $ret = $row [0];
  37. }
  38. Mssql_free_result ($cur);
  39. return $ret;
  40. }
  41. function Loadfirstrow ($sql) {
  42. if (! ( $cur = $this->justquery ($sql))) {
  43. return null;
  44. }
  45. $ret = null;
  46. if ($row = Mssql_fetch_object ($cur)) {
  47. $ret = $row;
  48. }
  49. Mssql_free_result ($cur);
  50. return $ret;
  51. }
  52. function Insertid () {
  53. Return mysql_insert_id ($this->link);
  54. }
  55. function query ($sql, $key = "", $returns = True, $batch = False) {
  56. $sqls = $result = Array ();
  57. Switch ($batch) {
  58. Default
  59. Case true:
  60. foreach ($sql as $index = = $query) {
  61. $this->queries[] = $query;
  62. $answer = Mssql_query ($query, $this->link);
  63. if (! $answer) {
  64. $this->errors[] = "n/a";//odbc_errormsg ($this->link);
  65. }
  66. else {
  67. if ($returns! = False) {
  68. if (Mssql_num_rows ($answer) > 0) {
  69. while ($row = Mssql_fetch_object ($answer)) {
  70. if ($key! = "") {
  71. $result [$index] [$row-$key] = $row;
  72. }
  73. else {
  74. $result [$index] = $row;
  75. }
  76. }
  77. } else {}
  78. } else {}
  79. }
  80. }
  81. Break
  82. Case false:
  83. $this->queries[] = $sql;
  84. $answer = Mssql_query ($sql, $this->link);
  85. if (! $answer) {
  86. $this->errors[] = "n/a";//odbc_errormsg ($this->link);
  87. $result = false;
  88. }
  89. else {
  90. if ($returns! = False) {
  91. if (Mssql_num_rows ($answer) > 0) {
  92. while ($row = Mssql_fetch_object ($answer)) {
  93. if ($key! = "") {
  94. $result [$row, $key] = $row;
  95. }
  96. else {
  97. $result [] = $row;
  98. }
  99. }
  100. } else {}
  101. }
  102. else {
  103. $result = true;
  104. }
  105. }
  106. Break
  107. }
  108. return $result;
  109. }
  110. function Loadobject ($sql, & $object) {
  111. if ($object! = null) {
  112. if (! ( $cur = $this->justquery ($sql))) {
  113. return false;
  114. } else {}
  115. if ($array = Mssql_fetch_array ($cur)) {
  116. Mssql_free_result ($cur);
  117. $this->bindarraytoobject ($array, $object);
  118. return true;
  119. }
  120. else {
  121. return false;
  122. }
  123. }
  124. else {
  125. if ($cur = $this->justquery ($sql)) {
  126. if ($object = Mssql_fetch_object ($cur)) {
  127. Mssql_free_result ($cur);
  128. return true;
  129. }
  130. else {
  131. $object = null;
  132. return false;
  133. }
  134. }
  135. else {
  136. return false;
  137. }
  138. }
  139. }
  140. function Bindarraytoobject ($array, & $obj) {
  141. if (!is_array ($array) | |!is_object ($obj)) {
  142. return (false);
  143. }
  144. foreach (Get_object_vars ($obj) as $k = = $v) {
  145. if (substr ($k, 0, 1) = = ' _ ') {
  146. $ak = $k;
  147. if (Isset ($array [$ak])) {
  148. $obj $k = $array [$ak];
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. function Formatcsvcell ($data) {
  155. $useQuotes = false;
  156. $quotable = Array (
  157. "" "" "" "" "",
  158. "," = ",",
  159. "
  160. "="
  161. "
  162. );
  163. foreach ($quotable as $char = = $repl) {
  164. if (eregi ($char, $data)) {
  165. $useQuotes = true;
  166. } else {}
  167. }
  168. if ($useQuotes = = True) {
  169. foreach ($quotable as $char = = $repl) {
  170. $data = Str_replace ($char, $repl, $data);
  171. }
  172. $data = "". $data. """;
  173. }
  174. else {
  175. }
  176. return $data;
  177. }
  178. }
  179. ?>
Copy Code
Object-oriented, PHP, MSSQL
  • Related Article

    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.