如何在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.

查看全部
doujiang1939
doujiang1939
2016/05/03 13:16
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

4个回复