面向对象的php操作mssql类

原创
2016-07-25 08:43:02590浏览
  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


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:PHP判断一张图片的主色调的代码片段 下一条:php操作redies封装的类

学习路径

查看更多

相关文章

查看更多