Rumah > pembangunan bahagian belakang > tutorial php > 面向对象的php操作mssql类

面向对象的php操作mssql类

WBOY
Lepaskan: 2016-07-25 08:43:02
asal
953 orang telah melayarinya
  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. ?>
复制代码

面向对象, php, mssql


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan