-
-
- defined('ACC')||exit('Access Denied');
- // Encapsulates mysql operation class, including connection function and query function.
- class mysql extends absdb{
- protected static $ins = null;
- protected $host; // Host name
- protected $user; // User name
- protected $passwd; // Password
- protected $db; // Database name
- protected $port; // Port
- protected $conn = null;
- // Internal operation, obtain an object
- public static function getIns() {
- if(self::$ins === null) {
- self::$ins = new self() ;
- }
- $conf = conf::getIns();
- self::$ins->host = $conf->host;
- self::$ins->user = $conf->user;
- self::$ins->passwd = $conf->pwd;
- self::$ins->db = $conf->db;
- self::$ins->port = $conf- >port;
- self::$ins->connect();
- self::$ins->select_db();
- self::$ins->setChar();
- return self::$ins ;
- }
- // Do not allow external new operations,
- protected function __construct() {
-
- }
- // Connect to the database
- public function connect() {
- $this->conn = @mysql_connect($this-> ;host,$this->user,$this->passwd,$this->port);
- if(!$this->conn) {
- $error = new Exception('The database cannot be connected' ,9);
- throw $error;
- }
- }
- // Send sql query
- public function query($sql) {
- $rs = mysql_query($sql,$this->conn);
- if(!$ rs) {
- log::write($sql);
- }
- return $rs;
- }
- // Encapsulate a getAll method
- // Parameters: $sql
- // Return: array,false
- public function getAll($ sql) {
- $rs = $this->query($sql);
- if(!$rs) {
- return false;
- }
- $list = array();
- while($row = mysql_fetch_assoc($rs )) {
- $list[] = $row;
- }
- return $list;
-
- }
- // Encapsulate a getRow method
- // Parameters: $sql
- // Return: array,false
- public function getRow($ sql) {
- $rs = $this->query($sql);
- if(!$rs) {
- return false;
- }
- return mysql_fetch_assoc($rs);
- }
- // Encapsulate a getOne method,
- // Parameters: $sql
- // Returns: int, str (single value)
- public function getOne($sql) {
- $rs = $this->query($sql);
- if(!$rs ) {
- return false;
- }
- $tmp = mysql_fetch_row($rs);
- return $tmp[0];
- }
- // Encapsulate an affect_rows() method
- // Parameters: None
- // Return int affected Number of rows
- public function affected_rows() {
- return mysql_affected_rows($this->conn);
- }
- // Return the value of the latest generated auto_increment column
- public function last_id() {
- return mysql_insert_id($this-> conn);
- }
- // Select library function
- public function select_db() {
- $sql = 'use ' . $this->db;
- return $this->query($sql);
- }
- / / Function to set character set
- public function setChar() {
- $sql = 'set names utf8';
- return $this->query($sql);
- }
- // Automatically generate insert statements, update statements and execute them
- public function autoExecute($data,$table,$act='insert',$where='') {
-
- if($act == 'insert') {
- $sql = 'insert into ' . $table . ' (';
- $sql .= implode(',',(array_keys($data)));
- $sql .= ') values ('';
- $sql .= implode("','",array_values ($data));
- $sql .= "')";
- } else if($act == 'update') {
- if(!trim($where)) {
- return false;
- }
-
- $ sql = 'update ' . $table . ' set ';
- foreach($data as $k=>$v) {
- $sql .= $k;
- $sql .= '=';
- $sql .= "'".$v."',";
- }
-
- $sql = substr($sql,0,-1);
- $sql .= ' where ';
- $sql .= $where;
- } else {
- return false;
- }
- //return $sql;
- return $this->query($sql);
-
- }
- }
-
Copy code
|