美文网首页
【php】Mysql访问类

【php】Mysql访问类

作者: 业余玩家 | 来源:发表于2020-07-01 23:01 被阅读0次
    <?php
    
    class SqlHelp{
        public $dbname=数据库;
        public $host="服务器";
        public $username="用户名";
        public $password="密码";
        public $conn;
    
        public function __construct(){
            $this->conn=mysqli_connect($this->host,$this->username,$this->password,$this->dbname);
            if(!$this->conn){
                die("数据库连接失败".mysqli_connect_error());
            }
            // mysqli_close();
        }
    
        //执行sql语句
        public function ExecuteSql($sql){
            $res=mysqli_query($this->conn,$sql);
            mysqli_close($this->conn);
            return $res;
        }
    
        //查询所有
        public function FetchAll($sql){
            $res=mysqli_query($this->conn,$sql);
            $res=mysqli_fetch_all($res,MYSQLI_ASSOC);
            mysqli_close($this->conn);
            // var_dump($res);
            return $res;
        }
        //查询一条
        public function FetchOne($sql){
            $res=mysqli_query($this->conn,$sql);
            $res=mysqli_fetch_assoc($res);
            mysqli_close($this->conn);
            // var_dump($res);
            return $res;
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:【php】Mysql访问类

          本文链接:https://www.haomeiwen.com/subject/khukqktx.html