Toc
  1. [0]=>,
  2. )
Toc
0 results found
Seven Chen
Mysql类 自己为求方便封装了一下
2008/02/13 NULL php

############################################################
# What’s this . This is a PHP5 CLASS to provide access utility
# for MySQL database.

# AUTHOR :: Humen1
# WEB SITE :: http://www.humen1.net
# Vision :: 1.0

############################################################

class mydb{
///start of the CLASS
private $host;
private $usr;
private $pwd;
private $db;
private $charset = NULL;
private $charsetArray = Array(“utf8”,”big5”,”gb2312”,”gbk”,”latin1”);
private $link;
private $result;
private $error = Array();
/**
#ERROR = Array(

[0]=>,

)

**/

###construct
function mydb($DB_host,$DB_user,$DB_passwd,$DB_name = “”){
$this->host = $DB_host;
$this->usr = $DB_user;
$this->pwd = $DB_passwd;
$this->db = $DB_name;
}

###set database name
function set_db($DB_name){
$this->db = $DB_name;
}

###get connection flag
function connection(){
///get connection
$this->link = @mysql_connect($this->host,$this->usr,$this->pwd);
if(!$this->link){
$this->error[0] = sprintf(“Error connecting to host %s by user %s”, $this->host,$this->usr);
}
///select db
if(!mysql_select_db($this->db,$this->link)){
$this->error[1] = sprintf(“Error in selecting %s database.\n mysql errno is %d and error message is %s”,$this->db,mysql_errno($this->link),mysql_error($this->link));
}
}

###set query charset
function set_charset($Charset){
if(in_array($Charset,$this->charsetArray)){
$this->charset = $Charset;
}
}

###Quick execute the statement
function query($SQL){
$this->connection();
if($this->charset !== NULL){
mysql_query(“SET NAMES “.$this->charset , $this->link);
}

$this->result = mysql_query($SQL , $this->link);
if(!$this->result){
$this->error[2] = sprintf(“Error query in %s database.\n mysql errno is %d and error message is %s”,$this->db,mysql_errno($this->link),mysql_error($this->link));
}else{
return $this->result;
}
}

###fetch the result as array
function fetch($result_type = MYSQL_BOTH){
///MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH
if($result_type == “1”) $result_type = MYSQL_NUM;
if($result_type == “A”) $result_type = MYSQL_ASSOC;

if(!$this->result){
$this->error[3] = “Error fetch array in %s database.There is no Result”;
}else{
return mysql_fetch_array($this->result,$result_type);
}
}

###fetch the result as object
function fetch_obj(){
return ($this->result) ? @mysql_fetch_object($this->result) : false;
}

###return the next id
function next_id(){
return ($this->link) ? @mysql_insert_id($this->link) : false;
}

###return the affectde rows
function affected_rows(){
return ($this->link) ? @mysql_affected_rows($this->link) : false;
}

###return the rows number from result
function num_rows(){
return ($this->result) ? @mysql_num_rows($this->result) : false;
}

###return the fields number from result
function num_fields(){
return ($this->result) ? @mysql_num_fields($this->result) : false;
}

###free the result
function free(){
return ($this->result) ? @mysql_free_result($this->result) : false;
}

/****************Advanced Plus************************/

###get one record only
function getOne($SQL){
$SQL .= “ LIMIT 1”;
$this->query($SQL);
return ($this->result) ? @mysql_fetch_row($this->result) : false;
}

///End of the CLASS
}
?>

humen1 Tech

本文作者:Seven Chen
版权声明:本文首发于Seven Chen的博客,转载请注明出处!