Discuz教程网

一个PHP Mysql类 可以参考学习熟悉下

[复制链接]
authicon dly 发表于 2011-9-3 19:44:25 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. class Mysql
  3. {
  4. private $conn;
  5. private $host;
  6. private $username;
  7. private $password;
  8. private $dbname;
  9. private $pconnect;
  10. private $charset;

  11. public function __construct(array $params = null)
  12. {
  13. if (!empty($params)) {
  14. foreach ($params as $k => $v) {
  15. $this->$k = $v;
  16. }
  17. }
  18. }

  19. public function connect()
  20. {
  21. $fun = $this->pconnect ? 'mysql_pconnect' : 'mysql_connect';
  22. $this->conn = $fun($this->host, $this->username, $this->password);
  23. $this->conn && $this->query('set names ' . $this->charset);
  24. $this->conn && mysql_select_db($this->dbname, $this->conn);
  25. }

  26. public function getInstance()
  27. {
  28. return $this->conn;
  29. }

  30. public function query($sql)
  31. {
  32. return mysql_query($sql, $this->conn);
  33. }

  34. public function fetchOne($sql)
  35. {
  36. $data = $this->fetchRow($sql);
  37. return $data[0];
  38. }

  39. public function fetchCol($sql)
  40. {
  41. $tmp = $this->fetchAll($sql, MYSQL_NUM);
  42. foreach ($tmp as $v) {
  43. $data[] = $v[0];
  44. }
  45. }

  46. public function fetchRow($sql)
  47. {
  48. $result = $this->query($sql);
  49. $data = mysql_fetch_row($result);
  50. mysql_free_result($result);
  51. return $data;
  52. }

  53. public function fetchAssoc($sql)
  54. {
  55. $result = $this->query($sql);
  56. $data = mysql_fetch_assoc($result);
  57. mysql_free_result($result);
  58. return $data;
  59. }

  60. public function fetchAll($sql, $type = MYSQL_ASSOC)
  61. {
  62. $result = $this->query($sql);
  63. while ($tmp = mysql_fetch_array($result, $type)) {
  64. $data[] = $tmp;
  65. }
  66. return $data;
  67. }

  68. public function fetchPairs($sql)
  69. {
  70. $result = $this->query($sql);
  71. while ($tmp = mysql_fetch_row($result)) {
  72. $data[$tmp[0]] = $tmp[1];
  73. }
  74. return $data;

  75. }

  76. public function insert($table, array $bind)
  77. {
  78. $cols = array();
  79. $vals = array();
  80. foreach ($bind as $col => $val) {
  81. $cols[] = $col;
  82. $vals[] = $val;
  83. unset($bind[$col]);
  84. }
  85. $sql = "INSERT INTO "
  86. . $table
  87. . ' (`' . implode('`, `', $cols) . '`) '
  88. . 'VALUES (\'' . implode('\', \'', $vals) . '\')';

  89. $stmt = $this->query($sql, $this->conn);
  90. $result = $this->affectedRows();
  91. return $result;
  92. }

  93. public function getLastInsertId()
  94. {
  95. return mysql_insert_id($this->conn);
  96. }

  97. public function affectedRows()
  98. {
  99. return mysql_affected_rows($this->conn);
  100. }

  101. public function update($table, array $bind, $where = '')
  102. {
  103. $set = array();
  104. foreach ($bind as $col => $val) {
  105. $set[] = '`' . $col . "` = '" . $val . "'";
  106. }

  107. $sql = "UPDATE `"
  108. . $table
  109. . '` SET ' . implode(', ', $set)
  110. . (($where) ? " WHERE $where" : '');

  111. $stmt = $this->query($sql, array_values($bind));
  112. $result = $this->affectedRows();
  113. return $result;
  114. }

  115. public function delete($table, $where = '')
  116. {
  117. /**
  118. * Build the DELETE statement
  119. */
  120. $sql = "DELETE FROM "
  121. . $table
  122. . (($where) ? " WHERE $where" : '');

  123. /**
  124. * Execute the statement and return the number of affected rows
  125. */
  126. $stmt = $this->query($sql);
  127. $result = $stmt ? mysql_affected_rows($this->conn) : $stmt;
  128. return $result;
  129. }

  130. public function close()
  131. {
  132. $this->conn && mysql_close($this->conn);
  133. }
  134. }
  135. ?>
复制代码





上一篇:PHP discuz分页函数multi()
下一篇:discuz7 PHPMysql操作类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 18:15

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表