<?php
class MySql{
var $db_name = '';
var $db_user ='';
var $db_password = '';
var $db_host = '';
var $db_charset = '';
var $connection_id = '';
var $pconnect = 0;
var $result;
//构造方法
public function __construct(){
$this->connect();
}
//数据库连接
public function connect(){
$this->db_name = DB_DATABASE;
$this->db_user = DB_USER;
$this->db_password = DB_PASSWORD;
$this->db_host = DB_HOST;
$this->db_charset = DB_CHARSET;
if ($this->pconnect) {
//永久链接
$this->connection_id = mysql_pconnect($this->db_host, $this->db_user, $this->db_password);
}
else {
//即使链接
$this->connection_id = mysql_connect($this->db_host, $this->db_user, $this->db_password);
}
if (!$this->connection_id) {
$this->halt('Can not connect to MySQL server!');
}
if (!@mysql_select_db($this->db_name, $this->connection_id)) {
$this->halt('Can not connect to MySQL Database:'.$this->db_name.'!');
}
@mysql_query("SET NAMES '{$this->db_charset}'", $this->connection_id);
}
//插入操作
public function insert($table, $values) {
$sql = "INSERT INTO {$table}";
$keysql = '';
$valuesql = '';
foreach($values as $key => $value) {
$keysql .= "`$key`,";
if (preg_match ("/^[0-9]+$/", $value)) {
$valuesql .= "{$value},";
} else {
$valuesql .= "'{$value}',";
}
}
$sql = $sql.'('.substr($keysql, 0, -1).')VALUES('.substr($valuesql, 0, -1).')';
if(!$this->query($sql)) return false;
return true;
}
//更新操作
public function update($table, $values, $where) {
$sql = "UPDATE {$table} SET ";
$keysql = '';
$valuesql = '';
foreach($values as $key => $value) {
if (preg_match ("/^[0-9]+$/", $value)) {
$sql .= "`$key`={$value},";
} else {
$sql .= "`$key`='{$value}',";
}
}
$sql = substr($sql, 0, -1);
$sql .= " WHERE {$where}";
return $this->query($sql);
}
//删除操作
public function delete($table, $where = '') {
$sql = "DELETE FROM {$table}";
if($where != '') $sql .= " WHERE {$where}";
if(!$this->query($sql)) return false;
return true;
}
//查询操作
public function select($what, $table, $where = '', $orderby = '', $limit = ''){
$sql = "SELECT {$what} FROM {$table}";
if ($where != '') $sql .= " WHERE {$where}";
if ($orderby != '') $sql .= " ORDER BY {$orderby}";
if ($limit != '') $sql .= " LIMIT {$limit}";
$this->query($sql);
return $this->fetch_array();
}
//执行操作 crud
public function query($sql){
$this->result = mysql_query($sql, $this->connection_id);
if (!$this->result) {
$this->halt('This query method has an error, please see error!', $sql);
}
return $this->result;
}
//结果集作为关联数组
public function fetch_array(){
$rows = array ();
while ($row = mysql_fetch_array($this->result)) {
array_push($rows, $row);
}
$this->free();
return $rows;
}
// 根据select查询结果计算结果集条数
public function db_num_rows(){
return mysql_num_rows($this->result);
}
// 根据insert,update,delete执行结果取得影响行数
public function db_affected_rows(){
return mysql_affected_rows($this->result);
}
/*取得上一步 INSERT 操作产生的 id*/
public function insert_id(){
return mysql_insert_id($this->result);
}
//释放结果集
public function free(){
//前面@符号 不显示错误信息
@mysql_free_result($this->result);
}
//错误提示
private function error() {
//返回上一个 MySQL 操作产生的文本错误信息
return (($this->connection_id) ? mysql_error($this->connection_id) : mysql_error());
}
private function errno() {
//返回上一个 MySQL 操作中的错误信息的数字编码
return intval(($this->connection_id) ? mysql_errno($this->connection_id) : mysql_errno());
}
private function halt($msg = '', $sql = '') {
$dberror = $this->error();
$dberrno = $this->errno();
echo "<div style=\"font:11px/1.5em microsoft yahei,verdana,arial; background-color:#EBEBEB; padding:1em;\">
<b>MySQL Error</b><br />
<b>Message</b>: $msg<br />
<b>SQL</b>: $sql<br />
<b>Error</b>: $dberror<br />
<b>Errno.</b>: $dberrno<br />
</div>";
exit();
}
//析构函数,自动关闭数据库 垃圾回收机制
public function __destruct()
{
if($this->result){
$this->free();
}
if ($this->connection_id) {
@mysql_close( $this->connection_id );
}
}
}
?>
<?php
/**
* mysql操作类(CRUD)
* C:增加(create)
* R:查询(retrieve)
* U:更新(update)
* D:删除(delete)
* @author :yanli_xu@163.com
*/
class Mysql{
var $db_name = '';
var $db_user ='';
var $db_password = '';
var $db_host = '';
var $db_charset = '';
var $connection_id = '';
var $pconnect = 0;
var $result;
//构造方法
public function __construct(){
$this->connect();
}
//数据库连接
private function connect(){
$this->db_name = DB_DATABASE;
$this->db_user = DB_USER;
$this->db_password = DB_PASSWORD;
$this->db_host = DB_HOST;
$this->db_charset = DB_CHARSET;
if ($this->pconnect) {
//永久链接
$this->connection_id = mysql_pconnect($this->db_host, $this->db_user, $this->db_password);
}
else {
//即使链接
$this->connection_id = mysql_connect($this->db_host, $this->db_user, $this->db_password);
}
if (!$this->connection_id) {
$this->halt('Can not connect to MySQL server!');
}
if (!@mysql_select_db($this->db_name, $this->connection_id)) {
$this->halt('Can not connect to MySQL Database:'.$this->db_name.'!');
}
@mysql_query("SET NAMES '{$this->db_charset}'", $this->connection_id);
}
//插入操作
public function insert($table, $values){
$sql = "INSERT INTO {$table}";
$keysql = '';
$valuesql = '';
foreach ($values as $k => $v) {
$keysql .= "{$k},";
if (preg_match ("/^[0-9]+$/", $v)) {
$valuesql .= "{$v},";
} else {
$valuesql .= "'{$v}',";
}
}
$sql = $sql . '(' . substr($keysql, 0, -1) . ')VALUES(' . substr($valuesql, 0, -1) . ')';
if(!$this->query($sql)) return false;
return true;
}
//查询操作
public function select($what = '', $table, $where = '', $orderby = '', $limit = ''){
$sql = "SELECT {$what} FROM {$table}";
if ($where != '') $sql .= " WHERE {$where}";
if ($orderby != '') $sql .= " ORDER BY {$orderby}";
if ($limit != '') $sql .= " LIMIT {$limit}";
$this->query($sql);
return $this->fetch_array();
}
//更新操作
public function update($table, $values, $where){
$sql = "UPDATE {$table} SET ";
foreach ($values as $k => $v) {
if (preg_match ("/^[0-9]+$/", $v)) {
$sql .= "$k={$v},";
} else {
$sql .= "$k='{$v}',";
}
}
$sql = substr ($sql, 0, - 1);
$sql .= " WHERE {$where}";
if(!$this->query($sql)) return false;
return true;
}
//删除操作
public function delete($table, $where){
$sql = "DELETE FROM {$table}";
if ($where != '')
$sql .= " WHERE {$where}";
if(!$this->query($sql)) return false;
return true;
}
// 根据select查询结果计算结果集条数
public function db_num_rows(){
return mysql_num_rows($this->result);
}
// 根据insert,update,delete执行结果取得影响行数
public function db_affected_rows(){
return mysql_affected_rows($this->result);
}
/*取得上一步 INSERT 操作产生的 id*/
public function insert_id(){
return mysql_insert_id($this->result);
}
//执行操作 crud
private function query($sql){
$this->result = mysql_query($sql, $this->connection_id);
if (!$this->result) {
$this->halt('This query method has an error, please see!', $sql);
}
return $this->result;
}
//结果集作为关联数组
public function fetch_array(){
$rows = array ();
while ($row = mysql_fetch_array($this->result)) {
array_push($rows, $row);
}
$this->free();
return $rows;
}
//释放结果集
public function free(){
//前面@符号 不显示错误信息
@mysql_free_result($this->result);
}
//错误提示
private function error() {
//返回上一个 MySQL 操作产生的文本错误信息
return (($this->connection_id) ? mysql_error($this->connection_id) : mysql_error());
}
private function errno() {
//返回上一个 MySQL 操作中的错误信息的数字编码
return intval(($this->connection_id) ? mysql_errno($this->connection_id) : mysql_errno());
}
private function halt($msg = '', $sql = '') {
$dberror = $this->error();
$dberrno = $this->errno();
echo "<div style=\"font:11px/1.5em microsoft yahei,verdana,arial; background-color:#EBEBEB; padding:1em;\">
<b>MySQL Error</b><br />
<b>Message</b>: $msg<br />
<b>SQL</b>: $sql<br />
<b>Error</b>: $dberror<br />
<b>Errno.</b>: $dberrno<br />
</div>";
exit();
}
//析构函数,自动关闭数据库 垃圾回收机制
public function __destruct()
{
if($this->result){
$this->free();
}
if ($this->connection_id) {
@mysql_close( $this->connection_id );
}
}
}
?>