首頁  >  文章  >  後端開發  >  物件導向的mysql資料庫操作php類

物件導向的mysql資料庫操作php類

WBOY
WBOY原創
2016-07-25 08:43:04800瀏覽
?>
  1. 類別資料庫{
  2. var $host = NULL;
  3. var $username = NULL;
  4. var $password = NULL ;
  5. var $databaseName = NULL;
  6. var $link = NULL;
  7. var $queries = NULL;
  8. var $errors = NULL;
  9. var $ULLbase
  10. function __construct($host, $username, $password, $database) {
  11. $this->database($host, $username, $password, $database);
  12. }
  13. 函數資料庫($host, $username, $password, $database) {
  14. /*$this->database = array (
  15. "host" => $host,
  16. "使用者名稱" => $使用者名稱,
  17. "密碼" => $密碼,
  18. "資料庫" => $資料庫,
  19. "連結" => "查詢" => array (),
  20. "errors " => array ()
  21. );*/
  22. $this->host = $host;
  23. $this-> ;使用者名稱= $使用者名稱;
  24. $this-> password = $password;
  25. $this->databaseName = $database;
  26. $this->link = "";
  27. $this ->queries = array ();
  28. $this->errors = array ();
  29. $this->databaseExtras = new stdClass;
  30. $this-> link = mysql_connect($this->host, $this->username, $this-> password) or die("無法連線到資料庫");
  31. mysql_select_db($this->databaseName);
  32. }
  33. function justquery($sql) {
  34. $this->queries [] = $sql;
  35. return mysql_query($sql, $this->link);
  36. }
  37. function loadResult($sql) {
  38. if (!($cur = $ this->justquery($sql))) {
  39. return null;
  40. }
  41. $ret = null;
  42. if ($row = mysql_fetch_row( $cur )) {
  43. $ret = $row[0];
  44. }
  45. mysql_free_result( $cur );
  46. return $ret;
  47. }
  48. function loadFirstRow($sql) {
  49. function loadFirstRow($sql) {
  50. if (!(> if (!(> if (!(> if (!(>) $cur = $this->justquery($sql))) {
  51. return null;
  52. }
  53. $ret = null;
  54. if ($row = mysql_fetch_object( $cur )) {
  55. $ret = $row;
  56. }
  57. mysql_free_result( $cur );
  58. return $ret;
  59. }
  60. function insertid() {
  61. return mythis_ins_ return idsql(my)(),sql ->link );
  62. }
  63. function query($sql , $key = "", $returns = true, $batch = false) {
  64. $result = array ();
  65. switch ($batch) {
  66. 預設值:
  67. case true :
  68. foreach ($sql as $index => $query) {
  69. $this->queries[] = $ query;
  70. $answer = mysql_query($query, $this->link);
  71. if (!$answer) {
  72. $this->errors[] = mysql_error($this-> link);
  73. }
  74. else {
  75. if ($returns != false) {
  76. if (mysql_num_rows ($answer) > 0){
  77. while ($row = mysql_fansetch_object($ )) {
  78. if ($key != ""){
  79. $結果[$index][$row- >$key] = $row;
  80. }
  81. else {
  82. $結果[$index][] = $row;
  83. }
  84. }
  85. } else {}
  86. } else {}
  87. }
  88. }
  89. 中斷;
  90. case false:
  91. $this->queries[] = $sql;
  92. $answer = mysql_query( $sql, $this->link);
  93. if (!$answer) {
  94. $this->errors[] = mysql_error($this->link);
  95. $ result = false;
  96. }
  97. else {
  98. if ($returns != false) {
  99. if (mysql_num_rows($answer) >; 0){
  100. while ($row = mysql_fetch_object($answer)) {
  101. if ($key != ""){
  102. $result[$row> $result[$ ->$key] = $row;
  103. }
  104. else {
  105. $result[] = $row;
  106. }
  107. }
  108. } else {}
  109. }
  110. else {
  111. $result = true;
  112. }
  113. }
  114. break;
  115. }
  116. return $result;
  117. }
  118. return $result;
  119. }
  120. function loadObject( $sql, &$object ) {
  121. if ($object != null) {
  122. if (!($cur = $this->justquery($sql))) {
  123. return false ;
  124. } else {}
  125. if ($array = mysql_fetch_assoc( $cur )) {
  126. mysql_free_result( $cur );
  127. $this->bindArrayObject( $array, $object);
  128. return true;
  129. }
  130. else {
  131. return false;
  132. }
  133. }
  134. else {
  135. if ($cur = $this->justque($ ) )) {
  136. if ($object = mysql_fetch_object( $cur )) {
  137. mysql_free_result( $cur );
  138. return true;
  139. }
  140. else { $ ;
  141. return false;
  142. }
  143. }
  144. else {
  145. return false;
  146. }
  147. }
  148. }
  149. function bindarrayObject( & $obj) {
  150. if (!is_array( $array ) || !is_object( $obj )) {
  151. return (false);
  152. }
  153. foreach (get_object_vars(object_vars($object_vars(object_vars(object_vars(object_vars(object_vars(object_vars)(object_vars(object_) ) as $k => $v) {
  154. if( substr( $k, 0, 1 ) != '_' ) {
  155. $ak = $k;
  156. if (isset($array[ $ ak])) {
  157. $obj->$k = $array[$ak];
  158. }
  159. }
  160. }
  161. 回傳true;
  162. }
  163. }
  164. 函數格式CSVCell($data) {
  165. $useQuotes = false;
  166. $quotable = array (
  167. """ => """",
  168. "," = > ",",
  169. "n" =>; "n"
  170. );
  171. foreach ($quotable as $char => $repl) {
  172. if (eregi($ char , $data)) {
  173. $useQuotes = true ;
  174. } else {}
  175. }
  176. if ($useQuotes == true) {
  177. foreach ($quotable as $ char => $repl) {
  178. $data = str_replace($char, $repl, $data);
  179. }
  180. $data = """ 。$資料。""";
  181. }
  182. else {
  183. }
  184. 回傳$data;
}
}?>

}
複製程式碼
物件導向, mysql, php

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn