Heim >Backend-Entwicklung >PHP-Tutorial >面向对象的mysql数据库操作php类

面向对象的mysql数据库操作php类

WBOY
WBOYOriginal
2016-07-25 08:43:04825Durchsuche
  1. class database {
  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 $databaseExtras = NULL;
  10. function __construct($host, $username, $password, $database) {
  11. $this->database($host, $username, $password, $database);
  12. }
  13. function database($host, $username, $password, $database) {
  14. /*$this->database = array (
  15. "host" => $host,
  16. "username" => $username,
  17. "password" => $password,
  18. "database" => $database,
  19. "link" => "",
  20. "queries" => array (),
  21. "errors" => array ()
  22. );*/
  23. $this->host = $host;
  24. $this->username = $username;
  25. $this->password = $password;
  26. $this->databaseName = $database;
  27. $this->link = "";
  28. $this->queries = array ();
  29. $this->errors = array ();
  30. $this->databaseExtras = new stdClass;
  31. $this->link = mysql_connect($this->host, $this->username, $this->password) or die("Could not connect to Database");
  32. mysql_select_db($this->databaseName);
  33. }
  34. function justquery($sql) {
  35. $this->queries[] = $sql;
  36. return mysql_query($sql, $this->link);
  37. }
  38. function loadResult($sql) {
  39. if (!($cur = $this->justquery($sql))) {
  40. return null;
  41. }
  42. $ret = null;
  43. if ($row = mysql_fetch_row( $cur )) {
  44. $ret = $row[0];
  45. }
  46. mysql_free_result( $cur );
  47. return $ret;
  48. }
  49. function loadFirstRow($sql) {
  50. 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 mysql_insert_id( $this->link );
  62. }
  63. function query($sql, $key = "", $returns = true, $batch = false) {
  64. $result = array ();
  65. switch ($batch) {
  66. default:
  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_fetch_object($answer)) {
  78. if ($key != ""){
  79. $result[$index][$row->$key] = $row;
  80. }
  81. else {
  82. $result[$index][] = $row;
  83. }
  84. }
  85. } else {}
  86. } else {}
  87. }
  88. }
  89. break;
  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->$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. function loadObject( $sql, &$object ) {
  119. if ($object != null) {
  120. if (!($cur = $this->justquery($sql))) {
  121. return false;
  122. } else {}
  123. if ($array = mysql_fetch_assoc( $cur )) {
  124. mysql_free_result( $cur );
  125. $this->bindArrayToObject( $array, $object);
  126. return true;
  127. }
  128. else {
  129. return false;
  130. }
  131. }
  132. else {
  133. if ($cur = $this->justquery($sql)) {
  134. if ($object = mysql_fetch_object( $cur )) {
  135. mysql_free_result( $cur );
  136. return true;
  137. }
  138. else {
  139. $object = null;
  140. return false;
  141. }
  142. }
  143. else {
  144. return false;
  145. }
  146. }
  147. }
  148. function bindArrayToObject( $array, &$obj) {
  149. if (!is_array( $array ) || !is_object( $obj )) {
  150. return (false);
  151. }
  152. foreach (get_object_vars($obj) as $k => $v) {
  153. if( substr( $k, 0, 1 ) != '_' ) {
  154. $ak = $k;
  155. if (isset($array[$ak])) {
  156. $obj->$k = $array[$ak];
  157. }
  158. }
  159. }
  160. return true;
  161. }
  162. function formatCSVCell($data) {
  163. $useQuotes = false;
  164. $quotable = array (
  165. "\"" => "\"\"",
  166. "," => ",",
  167. "\n" => "\n"
  168. );
  169. foreach ($quotable as $char => $repl) {
  170. if (eregi($char, $data)) {
  171. $useQuotes = true;
  172. } else {}
  173. }
  174. if ($useQuotes == true) {
  175. foreach ($quotable as $char => $repl) {
  176. $data = str_replace($char, $repl, $data);
  177. }
  178. $data = "\"" . $data . "\"";
  179. }
  180. else {
  181. }
  182. return $data;
  183. }
  184. }
  185. ?>
复制代码

面向对象, mysql, php


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn