<?php
Defined (' ACC ') | | Exit (' Access Denied ');
Encapsulates the MySQL operation class, including the connection function, and the 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;
In-house operation, get 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 let external do new operations,
protected function __construct () {
}
Connecting to a database
Public Function connect () {
$this->conn = @mysql_connect ($this->host, $this->user, $this->passwd, $this->port);
if (! $this->conn) {
$error = new Exception (' Database Not connected ', 9);
Throw $error;
}
}
Send SQL query
Public Function Query ($sql) {
$rs = mysql_query ($sql, $this->conn);
if (! $rs) {
Log::write ($sql);
}
return $rs;
}
Encapsulation of 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;
}
Encapsulation of 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);
}
Encapsulates a GetOne method,
Parameters: $sql
Return: Int,str (single value)
Public Function GetOne ($sql) {
$rs = $this->query ($sql);
if (! $rs) {
return false;
}
$tmp = Mysql_fetch_row ($RS);
return $tmp [0];
}
Select Library function
Public Function select_db () {
$sql = ' use '. $this->db;
return $this->query ($sql);
}
Functions for setting character sets
Public Function SetChar () {
$sql = ' Set names UTF8 ';
return $this->query ($sql);
}
Automatically generate INSERT statements, UPDATE statements, and execute
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);
}
}
/*
$db = mysql::getins (' localhost ', ' root ', ' 111111 ', ' php0620 ');
Print_r ($DB);
*/