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

Sha*_*kit -1 php mysqli

/* 连接文件 */

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

/* dao 文件 */

    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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误警告:mysqli_query() 期望参数 1 为 mysqli,在第 13 行的 /opt/lampp/htdocs/ankit_demo/dao.php 中给出为空

警告:mysqli_error() 期望参数 1 为 mysqli,第 13 行 /opt/lampp/htdocs/ankit_demo/dao.php 中给出的 null

如何解决这个问题?请帮我解决这个问题。

Yog*_*oli 5

试试这个,if 条件和 where 条件都有问题。并且我们无法回显对象或无法将对象转换为字符串。

数据库连接.php:

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

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;
    }
   }
?>
Run Code Online (Sandbox Code Playgroud)

控制器.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");
    }
}
?>
Run Code Online (Sandbox Code Playgroud)