您的位置:威尼斯官方网站 > 威尼斯正规官网 > PHP mysql操作类程序

PHP mysql操作类程序

发布时间:2019-08-05 17:09编辑:威尼斯正规官网浏览(173)

     代码如下

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

        function sub_sql($str)
        {
            global $prefix;//全局函数,表前缀
            return str_replace("dede_",$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);
        }
    }
    ?>

    本文由威尼斯官方网站发布于威尼斯正规官网,转载请注明出处:PHP mysql操作类程序

    关键词:

上一篇:只好想一下解决方法

下一篇:没有了