class MySQL extends TxObject {
private $_user; //连接数据库用户名
private $_pwd; //连接密码
private $_host; //数据库地址
private $_conn; //数据库连接指针
private $_err; //返回错误信息
private $_db; //连接的数据库名
/**
* 构造方法
* @return Connect
*/
public function __construct($host,$user,$pwd,$db=""){
$this->Connect($host,$user,$pwd,$db);
}
/**
* 建立数据库连接
* @param string $host 服务器地址
* @param string $user 登陆用户名
* @param string $pwd 登陆密码
* @param string $db 数据库名称
*/
private function Connect($host,$user,$pwd,$db=""){
$this->_conn=@mysql_connect($host,$user,$pwd);
$this->_host=$host;
$this->_user=$user;
$this->_pwd=$pwd;
if(mysql_errno()){
$this->_err=mysql_error();
return ;
}
mysql_query("set names gb2312");
if(!StringIsNull($db)){
$this->selectDB($db);
}
}
/**
* 选择数据库
* @param string $db 数据库名称
*/
public function selectDB($db){
$this->_db=$db;
if(!StringIsNull($this->_db)){
@mysql_select_db($this->_db,$this->_conn);
if(mysql_errno()){
$this->_err=mysql_error();
return ;
}
}else{
$this->_err="请输入数据库名";
return ;
}
}
/**
* 获得错误信息
*/
public function getError(){
if($this->isDBError()){
$this->openError($this->_err,2);
}
}
/**
* 获得数据库名称
* @return 数据库名称
*/
public function getDBName(){
return $this->_db;
}
/**
* 判断是否产生错误信息
* @return boolean
*/
public function isDBError(){
return (isset($this->_err) && $this->_err!="") ? true : false;
}
/**
* 获得连接信息
* @return Mysql连接信息
*/
public function getConn(){
return "php:mysql://".$this->_host."/mysql?user=$this->_user&password=$this->_pwd";
}
/**
* 是否选择数据库
* @return boolean
*/
public function isDBSelect(){
return !empty($this->_db);
}
/**
* 获得结果集
* @param string $sql
* @return
*/
public function query($sql) {
if(StringIsNull($sql)){
$this->_err="没有输入SQL语句";
return false;
}
if(eregi("^select ",$sql)){
$sort=1;
}elseif (eregi("^delete ",$sql)){
$sort=2;
}elseif(eregi("^insert ",$sql)){
$sort=3;
}elseif(eregi("^update ",$sql)){
$sort=4;
}
$sql=$this->getSQL($sql,$sort);
$query = @mysql_query($sql,$this->_conn);
if(!$query){
$this->openError("请合查数据表前缀是否有误!",2);
return ;
}
if(mysql_errno()){
$this->_err=mysql_error();
return false;
}
if($sort==1){
return new Select_Result($query, $this->_conn);
}elseif($sort==2){
return new Delete_Result($query , $this->_conn);
}elseif($sort==3){
return new Insert_Result($query , $this->_conn);
}elseif($sort==4){
return new Update_Result($query , $this->_conn);
}
}
/**
* 关闭数据源
*/
private function close(){
if($this->_conn){
@mysql_close($this->_conn);
unset($this->_conn);
unset($this->_host);
unset($this->_db);
unset($this->_user);
unset($this->_pwd);
unset($this->_err);
}
}
/**
* 析构函数
*/
public function __destruct(){
$this->close();
}
}
class Select_Result extends TxObject {
private $_query; // 数据查询结果
private $_conn; // 数据连接引用
private $_array; // 当前指针所指数据的数组
private $_pos; // 当前指针位置
private $_empty; // 判断这个结果集是否为空
/**
* 构造方法
* @param int $query query 字串
* @param int $conn 开启的数据连接
* @return DB_Result
*/
public function __construct(& $query , & $conn) {
$this->_conn=& $conn;
$this->_array = @mysql_fetch_array($query);
if (is_array($this->_array) && count($this->_array) != 0) {
$this->_query = $query;
$this->_pos = 1;
$this->_empty = false;
} else {
$this->_empty = true;
}
}
/**
* 判断是否有下一条数据库存在,同时指针移动到下一条数据
* @return boolean
*/
public function nextData() {
if($this->isEnd())
return false;
$tmp = @mysql_fetch_array($this->_query);
if (!ArrayIsNull($tmp)) {
$this->_array = $tmp;
$this->_pos++;
return true;
}
return false;
}
/**
* 指针移动到指定的位置
* @param int $pos
* @return boolean
*/
public function moveTo($pos) {
if ($this->_empty)
return false;
if (!is_int($pos) || $pos < 1)
if (!@mysql_data_seek($this->_query, $pos - 1))
return false;
$tmp = @mysql_fetch_array($this->_query);
if ($tmp) {
$this->_array = $tmp;
$this->_pos = $pos;
return true;
} else
return false;
}
/**
* 返回当前结果集中的总数量
* @return int
*/
public function getTotal() {
if ($this->_empty){
return 0;
}else{
$total = @mysql_num_rows($this->_query);
return $total;
}
}
/**
* 是否结尾
* @return boolean
*/
private function isEnd() {
if ($this->_empty){
return true;
}else{
return ($this->_pos == $this->getTotal());
}
}
/**
* 返回当前指针的位置
* @return int
*/
public function getPos() {
if ($this->_empty){
return -1;
}else{
return $this->_pos;
}
}
/**
* 取得当前指针位置的字段值(通过索引值或索引名称来取得)
* @param string|int $pname
* @return 字段值
*/
public function getValue($pname) {
if ($this->_empty)
return "";
if (!StringIsNull($pname) || is_int($pname))
return $this->_array[$pname];
else
return false;
}
/**
* 返回返回字段的数量,如果没有则返回0
* @return 字段数量
*/
public function getFieldNumber() {
if ($this->_empty){
return 0;
}else{
return @mysql_num_fields($this->_query);
}
}
/**
* 判断这个数据集是否为空
* @return boolean
*/
private function isEmpty() {
return $this->_empty;
}
/**
* 关闭查询结果(清除内存)
*/
private function close() {
if ($this->_query){
@mysql_free_result($this->_query);
unset($this->_conn);
unset($this->_query);
unset($this->_array);
unset($this->_pos);
unset($this->_empty);
}
}
public function __destruct(){
$this->close();
}
}
class Delete_Result extends TxObject {
private $_conn; //数据连接引用
private $_query; //查询结果
private $_rownum; //记录行数
public function __construct(& $query , & $conn){
$this->_conn=& $conn;
$this->_query=& $query;
$this->_rownum=mysql_affected_rows($this->_conn);
}
public function delRows(){
return $this->_rownum;
}
private function close(){
@mysql_free_result($this->_query);
unset($this->_conn);
unset($this->_query);
unset($this->_rownum);
}
public function __destruct(){
$this->close();
}
}
class Insert_Result extends TxObject {
private $_conn; //数据连接引用
private $_query; //结果集
private $_insertid; //最后插入的ID
public function __construct(& $query , & $conn){
$this->_conn = & $conn;
$this->_query = & $query;
$this->_insertid = @mysql_insert_id($this->_conn);
}
private function close(){
if($this->_query){
@mysql_free_result($this->_query);
unset($this->_conn);
unset($this->_query);
unset($this->_insertid);
}
}
public function getId(){
return $this->_insertid;
}
public function __destruct(){
$this->close();
}
}
class Update_Result extends TxObject {
private $_conn; //数据连接引用
private $_query; //结果集
public function __construct(& $query , & $conn){
$this->_conn = & $conn;
$this->_query = & $query;
}
private function close(){
if($this->_query){
@mysql_free_result($this->_query);
unset($this->_conn);
unset($this->_query);
}
}
public function __destruct(){
$this->close();
}
}

