一个php的mysql操作类

发布时间:2020-08-02编辑:脚本学堂
一个php的mysql操作类,简单实用,供大家参考。

一个php的mysql操作类,简单实用,供大家参考。
 

复制代码 代码如下:

<?php
//数据库操作类
class db
{
    //SQL执行后的数据保存变量;
    var $db;
    //读取或设置当前数据的位置
    var $position=0;
    //执行sql语句并把结果保存为db变量中;

    function sub_sql($str)
    {
        global $prefix;//全局函数,表前缀
        return str_replace("#@__",$prefix,$str);
    }
    function Sql($str)
    {
        $str=$this->sub_sql($str);
        $result = mysql_query($str);
        $i=0;
        while($row = mysql_fetch_array($result))
        {
            $str_array[$i]=$row;
            $i++;
        }
        if(empty($str_array))
        {
            $str_array=array();
        }
        $this->db=$str_array;
    }
    //读取一条数据并把数据往后移一位,如果数据为空则返回为null;
    function Get_One()
    {
        $re=empty($this->db[$this->position])?null:$this->db[$this->position];
        $this->position=$re?$this->position+1:$this->position;
        return $re;
    }
    //判断是否数据读取到结尾了
    function Judge()
    {
        $re=empty($this->db[$this->position])?true:false;
        return $re;
    }
    //取得db里面的个数
    function Get_Num()
    {
        return count($this->db);
    }
    //更新数据库里面的数据,$t为表名,$v格式为数组格式,上标为字段名,下标为数据;$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
    function Set_Updata($t,$v,$w,$p=0)
    {
        $this->Sql($t);
        $v_str="";
        $w_str="";
        $f="";
        foreach($v as $key=>$vaule)
        {
            if(!is_numeric($key))
            {
                if(empty($v_str))
                {
                    $v_str=htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
                }else
                {
                    $v_str=$v_str.",".htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
                }
            }
        }
        switch($p)
        {
            case 0:
                $f="=";
                break;
            case 1:
                $f=">";
                break;
            case -1:
                $f="<";
                break;
        }
        if(!empty($f))
        {
            foreach($w as $key=>$vaule)
            {
                if(!is_numeric($key))
                {
                    if(empty($v_str))
                    {
                        $w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
                    }else
                    {
                        $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
                    }
                }
            }
        }
        $sql="UPDATE ".$t." SET ".$v_str." where ".$w_str;
        return $result = mysql_query($sql);
    }
    //删除一数据$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
    function Set_Del($t,$w,$p=0)
    {
        $this->sub_sql($t);
        $w_str="";
        $f="";
        switch($p)
        {
            case 0:
                $f="=";
                break;
            case 1:
                $f=">";
                break;
            case -1:
                $f="<";
                break;
        }
        if(!empty($f))
        {
            foreach($w as $key=>$vaule)
            {
                if(!is_numeric($key))
                {
                    if(empty($v_str))
                    {
                        $w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
                    }else
                    {
                        $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
                    }
                }
            }
        }
        $str="DELETE FROM ".$t." WHERE ".$w_str;
        return $result = mysql_query($str);
    }
    function Add($t,$v)
    {
        $this->sub_sql($t);
        $k_str="";
        $v_str="";
        foreach($v as $key=>$vaule)
        {
            if(!is_numeric($key)){
                if(empty($k_str))
                {
                    $k_str=htmlspecialchars($key);
                    $v_str="'".htmlspecialchars($vaule)."'";
                }else
                {
                    $k_str=$k_str.",".htmlspecialchars($key);
                    $v_str=$v_str.","."'".htmlspecialchars($vaule)."'";
                }
            }
        }
        $str="INSERT INTO ".$t."(".$k_str.")"."value(".$v_str.")";
        return $result = mysql_query($str);
    }
}
?>