Home >Backend Development >PHP Tutorial >Implement mysql class using singleton pattern

Implement mysql class using singleton pattern

WBOY
WBOYOriginal
2016-07-25 08:45:231041browse
  1. defined('ACC')||exit('Access Denied');
  2. // Encapsulates mysql operation class, including connection function and query function.
  3. class mysql extends absdb{
  4. protected static $ins = null;
  5. protected $host; // Host name
  6. protected $user; // User name
  7. protected $passwd; // Password
  8. protected $db; // Database name
  9. protected $port; // Port
  10. protected $conn = null;
  11. // Internal operation, obtain an object
  12. public static function getIns() {
  13. if(self::$ins === null) {
  14. self::$ins = new self() ;
  15. }
  16. $conf = conf::getIns();
  17. self::$ins->host = $conf->host;
  18. self::$ins->user = $conf->user;
  19. self::$ins->passwd = $conf->pwd;
  20. self::$ins->db = $conf->db;
  21. self::$ins->port = $conf- >port;
  22. self::$ins->connect();
  23. self::$ins->select_db();
  24. self::$ins->setChar();
  25. return self::$ins ;
  26. }
  27. // Do not allow external new operations,
  28. protected function __construct() {
  29. }
  30. // Connect to the database
  31. public function connect() {
  32. $this->conn = @mysql_connect($this-> ;host,$this->user,$this->passwd,$this->port);
  33. if(!$this->conn) {
  34. $error = new Exception('The database cannot be connected' ,9);
  35. throw $error;
  36. }
  37. }
  38. // Send sql query
  39. public function query($sql) {
  40. $rs = mysql_query($sql,$this->conn);
  41. if(!$ rs) {
  42. log::write($sql);
  43. }
  44. return $rs;
  45. }
  46. // Encapsulate a getAll method
  47. // Parameters: $sql
  48. // Return: array,false
  49. public function getAll($ sql) {
  50. $rs = $this->query($sql);
  51. if(!$rs) {
  52. return false;
  53. }
  54. $list = array();
  55. while($row = mysql_fetch_assoc($rs )) {
  56. $list[] = $row;
  57. }
  58. return $list;
  59. }
  60. // Encapsulate a getRow method
  61. // Parameters: $sql
  62. // Return: array,false
  63. public function getRow($ sql) {
  64. $rs = $this->query($sql);
  65. if(!$rs) {
  66. return false;
  67. }
  68. return mysql_fetch_assoc($rs);
  69. }
  70. // Encapsulate a getOne method,
  71. // Parameters: $sql
  72. // Returns: int, str (single value)
  73. public function getOne($sql) {
  74. $rs = $this->query($sql);
  75. if(!$rs ) {
  76. return false;
  77. }
  78. $tmp = mysql_fetch_row($rs);
  79. return $tmp[0];
  80. }
  81. // Encapsulate an affect_rows() method
  82. // Parameters: None
  83. // Return int affected Number of rows
  84. public function affected_rows() {
  85. return mysql_affected_rows($this->conn);
  86. }
  87. // Return the value of the latest generated auto_increment column
  88. public function last_id() {
  89. return mysql_insert_id($this-> conn);
  90. }
  91. // Select library function
  92. public function select_db() {
  93. $sql = 'use ' . $this->db;
  94. return $this->query($sql);
  95. }
  96. / / Function to set character set
  97. public function setChar() {
  98. $sql = 'set names utf8';
  99. return $this->query($sql);
  100. }
  101. // Automatically generate insert statements, update statements and execute them
  102. public function autoExecute($data,$table,$act='insert',$where='') {
  103. if($act == 'insert') {
  104. $sql = 'insert into ' . $table . ' (';
  105. $sql .= implode(',',(array_keys($data)));
  106. $sql .= ') values ​​('';
  107. $sql .= implode("','",array_values ($data));
  108. $sql .= "')";
  109. } else if($act == 'update') {
  110. if(!trim($where)) {
  111. return false;
  112. }
  113. $ sql = 'update ' . $table . ' set ';
  114. foreach($data as $k=>$v) {
  115. $sql .= $k;
  116. $sql .= '=';
  117. $sql .= "'".$v."',";
  118. }
  119. $sql = substr($sql,0,-1);
  120. $sql .= ' where ';
  121. $sql .= $where;
  122. } else {
  123. return false;
  124. }
  125. //return $sql;
  126. return $this->query($sql);
  127. }
  128. }
Copy code

mysql


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn