OOP将数据库的应用封装成一个数据库类 应用:增、删、改、查

时间:2022-01-25 09:52:40
<?php 
header("Content-Type:text/html;charset=utf-8");
/*
 * 1、将数据库的应用封装成一个数据库类。
   应用:增、删、改、查
 */
class DB{   
    //定义成员属性
    public $server; //数据库
    public $username; //用户名
    public $password; //密码
    public $dbname;   //数据库名称

    //构造方法
    function __construct($server,$username,$password,$dbname){
        $this->server = $server;
        $this->username = $username;
        $this->password = $password;
        $this->dbname = $dbname;
        //连接数据库
        $this->connect();
        //选择默认数据库
        $this->selectDb(); 
    }

    //1、链接数据库
    function connect(){
        $link = mysql_connect($this->server,
                              $this->username,
                              $this->password);
    }
    //2、选择默认数据库
    function selectDb(){
        mysql_select_db($this->dbname);
    }
    //3、操作 (增)
    function insert($tbname,$data){ 
        /*
         * 字段:username,password
         *  值: tom,     123
         * $data = array("username"=>"tom",
         *               "password"=>"123");
         * //获取$data数据中的字段名称        * 
         */ 
        //读取data数组中的键名称
        $data_keys = array_keys($data);
        //$data_keys = 
        //array("username","password");
        //$fields = username,password
        //将$data_keys数组转换成字符串
        $fields = implode(",",$data_keys);
        //读取出$data数组里面的值
        //'tom','123'
        $values = implode("','",$data);
        //$values = tom','123
        //拼装sql语句
        //双引号可以解析变量,使用".$values."进行拼接就好了
        $query = "insert into $tbname ($fields) values ('".$values."')";     
        $result = mysql_query($query);
        return $result;
    }

    //删除
    function delete($tbname,$where=""){
        //$where="";有一个默认值
        $query = "delete from $tbname";               
        //关键词where是否出现在sql语句中,取决于
        //where条件是否为空
        //若where条件为空,则,where关键词不出现
        //否则需要出现where关键词
        //例如:id>5       
        if($where!=""){
            $query = $query." where ".$where;
        }       
        $result = mysql_query($query);
        return $result;
    }
    //改
    function update($tbname,$data,$where=""){
        /*
         * $data=array("username"=>"tom",
         *             "password"=>"456");
         * username='tom',password='456'            
         * username='tom',password='456'
         */
        $sets = "";
        foreach($data as $key=>$value){
            //第一次不添加","
            //当验证$sets为空时,此时说明
            //是第一次进行连接
            if($sets!=""){
                $sets.=",";
            }
            $sets .= $key."='".$value."'";
        }       
        $query = "update $tbname set $sets";
        //where关键词出现与否取决于
        //where条件是否为空
        //仅当where条件不为空时,where关键词才出现
        if($where!=""){
            $query = $query." where ".$where;
        }  

        $result = mysql_query($query);
        return $result;
    }
    //查
    function select($tbname,$fields,$where="",$limit=""){
        $query = "select $fields from $tbname"; 
        //拼装上where
        if($where!=""){
            $query = $query." where ".$where;
        }

        //拼装上limit
        if($limit!=""){
            $query = $query." limit ".$limit;
        }


        $result = mysql_query($query);
        return $result;
    }

    function __destruct(){
        mysql_close();
    }

}
$db = new DB("localhost","root","root","cms");
//增 插入 insert($tbname,$data)
/*$data = array("username"=>"rose","password"=>"123");
$result = $db->insert("cms_user",$data);
if($result){
    echo "成功";
}else{
    echo "失败";
}*/
//删 测试删除功能 delete($tbname,$where)
/*$result = $db->delete("cms_user");
if($result){
    echo "成功";
}else{
    echo "失败";
}*/

//改 更新
/*$data=array("username"=>"jerry1","password"=>"456");
$result=$db->update("cms_user",$data,"id=8");

if($result){
    echo "成功";
}else{
    echo "失败";
}*/

//查 select($tbname,$fields,$where="",$limit="")
$result = $db->select("cms_user","id,username,password","","2");
//获取资源中的数据
while($row = mysql_fetch_assoc($result)){
    //将查询到的结果进行输出
    echo "id=".$row['id']."<br/>";
    echo "username=".$row['username']."<br/>";
    echo "password=".$row['password']."<br/>";
}