分类:
2009-06-03 14:51:08
/**
* filename: DB_Mysql.class.php
* @package:phpbean
* @author :feifengxlq<[email]feifengxlq@sohu.com[/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.1
* create:2006-5-30
* description:the interface of mysql.
*
* example:
* ////////////Select action (First mode)//////////////////////////////
try{
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->query("select * from test");
for($i=0;$i<$mysql->num_rows($rs);$i++)
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
}catch (Exception $e)
{
printf("%s",$e->__toString());
}
* ////////////Select action (Second mode)//////////////////////////////
try{
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->execute("select * from test");
print_r($rs);
$mysql->close();
}catch (Exception $e)
{
printf("%s",$e->__toString());
}
* /////////////insert action////////////////////////////
try {
$mysql=new DB_Mysql("localhost","root","root","root");
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
}catch (Exception $e)
{
printf("%s",$e->__toString());
}
*/
require_once(ROOT_PATH."/../library/libs/exception/DB_Mysql_Exception.class.php");
class DB_Mysql{
/* private: connection parameters */
private $host="localhost";
private $database="";
private $user="root";
private $password="";
/* private: configuration parameters */
private $pconnect=false;
private $debug=false;
/* private: result array and current row number */
private $link_id=0;
private $query_id=0;
private $record=array();
/**
* construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
*/
public function __construct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("password",$password);
$this->set("database",$database);
$this->connect();
}
/**
* set the value for the param of this class
*
* @param string $var
* @param string $value
*/
public function set($var,$value)
{
$this->$var=$value;
}
/**
* connect to a mysql server,and choose the database.
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
*/
public function connect($database="",$host="",$user="",$password="")
{
if(!empty($database))$this->set("database",$database);
if(!empty($host))$this->set("host",$host);
if(!empty($user))$this->set("user",$user);
if(!empty($password))$this->set("password",$password);
if($this->link_id==0)
{
if($this->pconnect)
$this->link_id=@mysql_pconnect($this->host,$this->user,$this->password);
else
$this->link_id=@mysql_connect($this->host,$this->user,$this->password);
if(!$this->link_id)
throw new DB_Mysql_Exception("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
throw new DB_Mysql_Exception("Mysql Select database Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
}
return $this->link_id;
}
/**
* query a sql into the database
*
* @param string $strsql
* @return query_id
*/
public function query($strsql="")
{
if(empty($strsql)) throw new DB_Mysql_Exception("Mysql Error:".__FUNCTION__."() strsql is empty!");
if($this->link_id==0) $this->connect();
if($this->debug) printf("Debug query sql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id) throw new DB_Mysql_Exception("Mysql query fail,Invalid sql:".$strsql.".");
return $this->query_id;
}
/**
* query a sql into the database,while it is differernt from the query() method,
* this method will return a record(array);
*
* @param string $strsql
* @param string $style
* @return $record is a array()
*/
public function Execute($strsql,$style="array")
{
$this->query($strsql);
if(!empty($this->record))$this->record=array();
$i=0;
if($style=="array"){
while ($temp=@mysql_fetch_array($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}else{
while ($temp=@mysql_fetch_object($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return $this->record;
}
/**
* seek,but not equal to mysql_data_seek. this methord will return a list.
*
* @param int $pos
* @param string $style
* @return record
*/
public function seek($pos=0,$style="array")
{
if(!@mysql_data_seek($this->query_id,$pos))
throw new DB_Mysql_Exception("Error in".__FUNCTION__."():can not seek to row ".$pos."!");
$result=@($style=="array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result) throw new DB_Mysql_Exception("Error in ".__FUNCTION__."():can not fetch data!");
return $result;
}
/**
* free the result of query
*
*/
public function free()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
}
/**
* evaluate the result (size, width)
*
* @return num
*/
public function affected_rows()
{
return @mysql_affected_rows($this->link_id);
}
public function num_rows()
{
return @mysql_num_rows($this->query_id);
}
public function num_fields()
{
return @mysql_num_fields($this->query_id);
}
public function insert_id()
{
return @mysql_insert_id($this->link_id);
}
public function close()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0) throw new DB_Mysql_Exception("Mysql Error:".mysql_errno().":".mysql_error());
}
public function select($strsql,$number,$offset)
{
if(empty($number)){
return $this->Execute($strsql);
}else{
return $this->Execute($strsql.' limit '.$offset.','.$number);
}
}
public function __destruct()
{
$this->close();
$this->set("user","");
$this->set("host","");
$this->set("password","");
$this->set("database","");
}
}
?>