Home  >  Article  >  Backend Development  >  PHP mysql operation program_PHP tutorial

PHP mysql operation program_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:21824browse

A good PHP mysql operation class, shared with everyone to learn and use.

The code is as follows Copy code

//Database processing class
class db
{
//Data storage variable after SQL execution;
var $db;
//Read or set the position of the current data
var $position=0;
//Execute the SQL statement and save the result as a db variable;

function sub_sql($str)
{
global $prefix;//Global function, table prefix
return str_replace("dede_",$prefix,$str);
}
function Sql($str)
{
$str=$this->sub_sql($str);
> while($row = mysql_fetch_array($result))
if(empty($str_array))
                                                                                                                                                                                                               $str_array=                              One bit, if the data is empty, it returns null;
function Get_One()
{
$re=empty($this->db[$this->position])?null:$ this->db[$this->position];
             $this->position=$re?$this->position+1:$this->position;
                                                                                                   
}
//Judge whether the data has been read to the end
function Judge()
{
$re=empty($this->db[$this->position] )?true:false;
              return $re; db);
}
//Update the data in the database, $t is the table name, $v format is the array format, the superscript is the field name, and the subscript is the data; $w is the condition and the superscript is the field The subscript is data, $p is the condition, 0 is the equal sign, 1 is greater than, -1 is less than;
function Set_Updata($t,$v,$w,$p=0)
{
        $this->Sql($t);
                                                                                                                                                                                      >$vaule)
If(!is_numeric($key))

                                                              $v_str=htmlspecialchars($key : htmlspecialchars($vaule)."'";
                                                                                                     0:
                      $f="=";
                                                                                                                              break;                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);
    }
}
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444651.htmlTechArticle一个不错的PHP mysql操作类 ,分享给大家学习使用。 代码如下 复制代码 ?php //数据库处理类 class db { //SQL执行后的数据保存变量; var $db; //读取...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn