class mysql {
var $linkid=null;
function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = 'gbk', $connect = 1) {
$this -> connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $connect);
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = 'gbk', $connect=1){
$func = empty($connect) ? 'mysql_pconnect' : 'mysql_connect';
if(!$this->linkid = @$func($dbhost, $dbuser, $dbpw, true)){
$this->dbshow('can not connect to mysql!');
} else {
if($this->dbversion() > '4.1'){
mysql_query( "set names gbk");
if($this->dbversion() > '5.0.1'){
mysql_query("set sql_mode = ''",$this->linkid);
}
}
}
if($dbname){
if(mysql_select_db($dbname, $this->linkid)===false){
$this->dbshow("can't select mysql database($dbname)!");
}
}
}
function select_db($dbname){
return mysql_select_db($dbname, $this->linkid);
}
function query($sql){
if(!$query=@mysql_query($sql, $this->linkid)){
$this->dbshow("query error:$sql");
}else{
return $query;
}
}
function getall($sql, $type=mysql_assoc){
$query = $this->query($sql);
while($row = mysql_fetch_array($query,$type)){
$rows[] = $row;
}
return $rows;
}
function getone($sql, $type=mysql_assoc){
$query = $this->query($sql,$this->linkid);
$row = mysql_fetch_array($query, $type);
return $row;
}
function fetch_array($result,$type = mysql_assoc){
return mysql_fetch_array($result);
}
function affected_rows(){
return mysql_affected_rows($this->linkid);
}
function num_rows(){
return mysql_num_rows($this->linkid);
}
function num_fields($result){
return mysql_num_fields($result);
}
function insert_id(){
return mysql_insert_id($this->linkid);
}
function free_result(){
return mysql_free_result($this->linkid);
}
function error(){
return mysql_error($this->linkid);
}
function errno(){
return mysql_errno($this->linkid);
}
function close(){
return mysql_close($this->linkid);
}
function dbversion(){
return mysql_get_server_info($this->linkid);
}
function dbshow($msg){
if($msg){
echo "error:".$msg."
";
}else{
echo "errno:".$this->errno()." error:".$this->error();
}
exit;
}
}
|