Posted By

MMDeveloper on 02/24/09


Tagged

database class php object oop mssql


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

umang_nine
Scooter
rezzz
tux-world


OOP MSSQL Database Class


 / Published in: PHP
 

Similar to my MySQL and MySQLi class but converted to be used with MS SQL

  1. <?php
  2.  
  3. /*
  4. untested
  5. */
  6.  
  7. class database_mssql {
  8.  
  9. var $database = NULL;
  10. var $sqls = NULL;
  11.  
  12.  
  13. var $host = NULL;
  14. var $username = NULL;
  15. var $password = NULL;
  16. var $databaseName = NULL;
  17. var $link = NULL;
  18. var $queries = NULL;
  19. var $errors = NULL;
  20.  
  21. function database_mssql($host, $username, $password, $database) {
  22.  
  23. $this->host = $host;
  24. $this->username = sha1($username);
  25. $this->password = sha1($password);
  26. $this->database = $database;
  27. $this->link = "";
  28. $this->queries = array ();
  29. $this->errors = array ();
  30.  
  31. $this->sqls = array ();
  32.  
  33. $this->link = mssql_connect($this->host, $username, $password);
  34. mssql_select_db($this->database, $this->link);
  35. }
  36.  
  37. function justquery($sql) {
  38. $this->queries[] = $sql;
  39. return mssql_query($sql, $this->link);
  40. }
  41.  
  42. function loadResult($sql) {
  43. if (!($cur = $this->justquery($sql))) {
  44. return null;
  45. }
  46. $ret = null;
  47. if ($row = mssql_fetch_row( $cur )) {
  48. $ret = $row[0];
  49. }
  50. mssql_free_result( $cur );
  51. return $ret;
  52. }
  53.  
  54. function loadFirstRow($sql) {
  55. if (!($cur = $this->justquery($sql))) {
  56. return null;
  57. }
  58. $ret = null;
  59. if ($row = mssql_fetch_object( $cur )) {
  60. $ret = $row;
  61. }
  62. mssql_free_result( $cur );
  63. return $ret;
  64. }
  65.  
  66. function insertid() {
  67. //return mysql_insert_id( $this->link );
  68. }
  69.  
  70. function query($sql, $key = "", $returns = true, $batch = false) {
  71. $sqls = $result = array ();
  72.  
  73. switch ($batch) {
  74. default:
  75. case true:
  76. foreach ($sql as $index => $query) {
  77. $this->queries[] = $query;
  78. $answer = mssql_query($query, $this->link);
  79.  
  80. if (!$answer) {
  81. $this->errors[] = "n/a";//odbc_errormsg($this->link);
  82. }
  83. else {
  84. if ($returns != false) {
  85. if (mssql_num_rows($answer) > 0){
  86. while ($row = mssql_fetch_object($answer)) {
  87. if ($key != ""){
  88. $result[$index][$row->$key] = $row;
  89. }
  90. else {
  91. $result[$index][] = $row;
  92. }
  93. }
  94. } else {}
  95. } else {}
  96. }
  97. }
  98. break;
  99.  
  100. case false:
  101. $this->queries[] = $sql;
  102. $answer = mssql_query($sql, $this->link);
  103.  
  104. if (!$answer) {
  105. $this->errors[] = "n/a";//odbc_errormsg($this->link);
  106. $result = false;
  107. }
  108. else {
  109. if ($returns != false) {
  110. if (mssql_num_rows($answer) > 0){
  111. while ($row = mssql_fetch_object($answer)) {
  112. if ($key != ""){
  113. $result[$row->$key] = $row;
  114. }
  115. else {
  116. $result[] = $row;
  117. }
  118. }
  119. } else {}
  120. }
  121. else {
  122. $result = true;
  123. }
  124. }
  125. break;
  126. }
  127.  
  128. return $result;
  129. }
  130.  
  131. function loadObject( $sql, &$object ) {
  132. if ($object != null) {
  133. if (!($cur = $this->justquery($sql))) {
  134. return false;
  135. } else {}
  136. if ($array = mssql_fetch_array( $cur )) {
  137. mssql_free_result( $cur );
  138. $this->bindArrayToObject( $array, $object);
  139. return true;
  140. }
  141. else {
  142. return false;
  143. }
  144. }
  145. else {
  146. if ($cur = $this->justquery($sql)) {
  147. if ($object = mssql_fetch_object( $cur )) {
  148. mssql_free_result( $cur );
  149. return true;
  150. }
  151. else {
  152. $object = null;
  153. return false;
  154. }
  155. }
  156. else {
  157. return false;
  158. }
  159. }
  160. }
  161.  
  162. function bindArrayToObject( $array, &$obj) {
  163. if (!is_array( $array ) || !is_object( $obj )) {
  164. return (false);
  165. }
  166.  
  167. foreach (get_object_vars($obj) as $k => $v) {
  168. if( substr( $k, 0, 1 ) != '_' ) {
  169. $ak = $k;
  170. if (isset($array[$ak])) {
  171. $obj->$k = $array[$ak];
  172. }
  173. }
  174. }
  175.  
  176. return true;
  177. }
  178.  
  179. function formatCSVCell($data) {
  180. $useQuotes = false;
  181.  
  182. $quotable = array (
  183. "\"" => "\"\"",
  184. "," => ",",
  185. "\n" => "\n"
  186. );
  187.  
  188. foreach ($quotable as $char => $repl) {
  189. if (eregi($char, $data)) {
  190. $useQuotes = true;
  191. } else {}
  192. }
  193.  
  194. if ($useQuotes == true) {
  195. foreach ($quotable as $char => $repl) {
  196. $data = str_replace($char, $repl, $data);
  197. }
  198.  
  199. $data = "\"" . $data . "\"";
  200. }
  201. else {
  202.  
  203. }
  204.  
  205. return $data;
  206. }
  207. }
  208. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: tinatina on December 3, 2010

Thx this is mega. it took me a while to understand, but anyway. how would your OOP-CRUD looks like.

You need to login to post a comment.