MySql操作类

最近写了一个简单的Mysql操作类,拿出来和大家分享一下,还请各位高手指正一下析构函数的用法:

<?php
/*
* Created on 2010-5-25
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*
* class mysql
* made by S71ENCE
*/

class mysql
{
    private $host;//主机名
    private $name;//用户名
    private $pwd;//密码
    private $dataBase;//数据库名
    private $coding;//编码

//初始化
    function __construct($host,$name,$pwd,$dataBase,$coding)
    {
        $this->host=$host;
        $this->name=$name;
        $this->pwd=$pwd;
        $this->dataBase=$dataBase;
        $this->coding=$coding;
        $this->connect();//初始化连接
    }

/*********************************************************************************************
* 数据库
* 基本方法
********************************************************************************************/

//数据库连接
    function connect()
    {
        $link=mysql_connect($this->host,$this->name,$this->pwd) or die($this-error());
        mysql_select_db($this->dataBase,$link) or die("无法连接数据库".$this->dataBase);
        mysql_query("set names '$this->coding'");
    }

//错误信息
    function error()
    {
        return mysql_error();
    }

//mysql_query()方法
    function query($sql, $type = '')
    {
        if(!($query = mysql_query($sql)))
        {
            $this->show('The SQL String:', $sql);
        }

        echo $sql."<br/>";
        return $query;
    }

//sql语句显示
    function show($message = '', $sql = '')
    {
        if(!$sql)
        {
            echo $message;
        }
        else
        {
            echo $message.'<br>'.$sql;
        }
    }

//mysql_affected_rows()方法
    function affected_rows()
    {
        return mysql_affected_rows();
    }

//mysql_result方法
    function result($query, $row)
    {
        return mysql_result($query, $row);
    }

//mysql_num_rows方法
    function num_rows($query)
    {
        return @mysql_num_rows($query);
    }

//mysql_num_fields方法
    function num_fields($query)
    {
        return mysql_num_fields($query);
    }

//mysql_free_result方法
    function free_result($query)
    {
        return mysql_free_result($query);
    }

//mysql_insert_id方法
    function insert_id()
    {
        return mysql_insert_id();
    }

//mysql_fetch_row方法
    function fetch_row($query)
    {
        return mysql_fetch_row($query);
    }

//mysql_get_server_info方法
    function version()
    {
        return mysql_get_server_info();
    }

//mysql_fetch_array()方法
    function fetch_array($result)
    {
        return mysql_fetch_array($result);
    }

//mysql_close方法
    function close()
    {
        return mysql_close();
    }


/*********************************************************************
* 数据库
* 功能方法
*********************************************************************/

/*
*    insert方法
*  $table 表名
*    $field 字段名
*    $value 字段值
*/

    function fn_insert($table,$field,$value)
    {
        $this->query("insert into $table ($field) values ($value)");
        $this->close();
    }


/*
*    select方法
*  $table 表名
*    $field 字段名
*    $condition 查询条件
*    $order 排序条件
*    $limit 取出条数
*/
    function fn_select($table,$field,$condition,$order,$limit)
    {
        $query="select $field from $table";

        if($condition!="")
        {
            $query.=" where $condition";
        }

        if($order!="")
        {
            $query.=" order by $order ";
        }

        if($limit!="")
        {
            $query.=" limit $limit";
        }

        return $this->query($query);
        $this->close();
    }


/*
*    delete方法
*    $table 表名
*    $field 字段名
*    $value 字段值
*/
    function fn_delete($table,$condition)
    {
        $this->query("delete from $table where $condition");
        $this->close();
    }


/*
*    update方法
*    $table 表名
*    $field 字段名
*    $value 字段值
*/
    function fn_update($table,$set,$condition)
    {
        $sql="update $table set $set";
        if($condition!="")
        {
            $sql.=" where $condition";
        }

        $this->query($sql);
        $this->close();
    }



/*
* 析构函数,垃圾回收
*/
    function __destruct()
    {

    }
}
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值