<?php 2 class DBHelper 3 { 4 private $link; 5 static private $_instance; 6 7 // 连接数据库 8 private function __construct($host, $username, $password) 9 { 10 $this->link = mysql_connect($host, $username, $password); 11 $this->query("SET NAMES 'utf8'", $this->link); 12 //echo mysql_errno($this->link) . ": " . mysql_error($link). "n"; 13 //var_dump($this->link); 14 return $this->link; 15 } 16 private function __clone() 17 { 18 } 19 public static function get_class_nmdb($host, $username, $password) 20 { 21 //$connector = new nmdb($host, $username, $password); 22 //return $connector; 23 24 if (FALSE == (self::$_instance instanceof self)) { 25 self::$_instance = new self($host, $username, $password); 26 } 27 return self::$_instance; 28 } 29 // 连接数据表 30 public function select_db($database) 31 { 32 $this->result = mysql_select_db($database); 33 return $this->result; 34 } 35 // 执行SQL语句 36 public function query($query) 37 { 38 return $this->result = mysql_query($query, $this->link); 39 } 40 // 将结果集保存为数组 41 public function fetch_array($fetch_array) 42 { 43 return $this->result = mysql_fetch_array($fetch_array, MYSQL_ASSOC); 44 } 45 // 获得记录数目 46 public function num_rows($query) 47 { 48 return $this->result = mysql_num_rows($query); 49 } 50 // 关闭数据库连接 51 public function close() 52 { 53 return $this->result = mysql_close($this->link); 54 } 55 } 56 $connector = DBHelper::get_class_nmdb($host, $username, $password); 57 $connector -> select_db($database); 58 ?>
博客
前端高频面试(1)
06-21
152
04-10
450
04-08
270
02-02
980
02-01
178
博客
Uniapp上传图片
01-09
395
12-11
437
12-11
436
11-27
494
10-19
207