如何在php中使用mvc连接数据库

/* connection file */

    class dbconnect{
        public function connect(){
            $host = 'localhost';
            $user = 'root';
            $pass = '';
            $db = 'demo';
            $connection = mysqli_connect($host,$user,$pass,$db); 
            return $connection;
        }
    }

/* dao file */

    include 'dbconnect.php';
    class dao extends dbconnect{
        private $conn; 
        function __dao(){ 
            $dbcon = new dbconnect();
            $conn = $dbcon->connect();
        }
        function select( $table , $where='' , $other='' ){
            if(!$where = '' ){
                $where = 'where' . $where;
            }
            $sele = mysqli_query($this->conn,"SELECT * FROM  $table $where $other") or die(mysqli_error($this->conn));
            echo $sele;
            return $sele;
        }
    }
/* controler file */

include 'dao.php';

$d = new dao();



if(isset($_POST['btn_login'])){
    extract($_POST);
    $username = $_POST['user_name'];
    $pswd = $_POST['pswd'];

    $sel = $d->select("users" , "email_id = '" . $username . "'AND password='" . $pswd . "'" ) or die('error from here');
    $result = mysqli_fetch_array($sel) ;

    if($result['email_id'] == $username && $result['password'] == $pswd){
        SESSION_START();
        $_SESSION['user_name'] = $result['email_id'];
        $_SESSION['message'] = 'Invalid Username Or Password';
        header("location:index.php");
    }
    else{
        $_SESSION['error'] = 'Invalid Username Or Password';
        // header("Location:login.php");
    }
}

I got an error Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /opt/lampp/htdocs/ankit_demo/dao.php on line 13

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /opt/lampp/htdocs/ankit_demo/dao.php on line 13

how to solve this? Please help me to solve out this.

Try this out, there was issues with if condition as well as the where condition. and we can't echo a object or can't convert object to string.

dbconnect.php:

<?php
class dbconnect{
    public function connect(){
         $host = 'localhost';
         $user = 'root';
         $pass = '';
         $db = 'demo';
         $connection = mysqli_connect($host,$user,$pass,$db); 
         return $connection;
     }
}

dao.php:

<?php
include 'dbconnect.php';
class dao extends dbconnect {
    private $conn; 
    public function __construct() { 
       $dbcon = new parent(); 
       // this is not needed in your case
       // you can use $this->conn = $this->connect(); without calling parent()
       $this->conn = $dbcon->connect();
    }

    public function select( $table , $where='' , $other='' ){
       if($where != '' ){  // condition was wrong
         $where = 'where ' . $where; // Added space 
       }
       $sql = "SELECT * FROM  ".$table." " .$where. " " .$other;
       $sele = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn));
       // echo $sele; // don't use echo statement because - Object of class mysqli_result could not be converted to string
       return $sele;
    }
   }
?>

controller.php:

<?php
include 'dao.php';

$d = new dao();

if(isset($_POST['btn_login'])){
    extract($_POST);
    $username = $_POST['user_name'];
    $pswd = $_POST['pswd'];

    $sel = $d->select("users" , "email_id = '" . $username . "' AND password='" . $pswd . "'" ) or die('error from here');
    $result = mysqli_fetch_array($sel) ;

    if($result['email_id'] == $username && $result['password'] == $pswd){
        SESSION_START();
        $_SESSION['user_name'] = $result['email_id'];
        $_SESSION['message'] = 'Invalid Username Or Password';
        header("location:index.php");
    }
    else{
        $_SESSION['error'] = 'Invalid Username Or Password';
        // header("Location:login.php");
    }
}
?>

Change name of your constructor from __dao() to __construct().

Replace your line 6-th line of code by:

$this->conn = $dbcon->connect();

try this :

include 'dbconnect.php';
    class dao extends dbconnect{
        private $conn; 
        function __construct(){ 
            $dbcon = new dbconnect();
            $this->conn = $dbcon->connect();
        }
        function select( $table , $where='' , $other='' ){
            if(!$where = '' ){
                $where = 'where' . $where;
            }
            $sql = "SELECT * FROM  ".$table." " .$where. " " .$other";
            $sele = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn));
            echo $sele;
            return $sele;
        }
    }

Connection file:

class dbconnect{
    public function connect(){
        $host = 'localhost';
        $user = 'root';
        $pass = '';
        $db = 'demo';
        $connection = mysqli_connect($host,$user,$pass,$db); 
        return $connection;
    }
}

dao file:

include 'dbconnect.php';
class dao extends dbconnect {
    private $conn; 
    public function __construct() { 
        $dbcon = new parent(); // this is not needed in your case
        // you can use $this->conn = $this->connect(); without calling parent()
        $this->conn = $dbcon->connect();
    }
    public function select( $table , $where='' , $other='' ){
        if(!$where = '' ){
            $where = 'where' . $where;
        }
        $sele = mysqli_query($this->conn,"SELECT * FROM  $table $where $other") or die(mysqli_error($this->conn));
        echo $sele;
        return $sele;
    }
}

But I think it would be better to use PDO or much better a ORM system like laravel eloquent.