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

时间:2016-05-03 13:16:46

标签: php mysqli

/ *连接文件* /

    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");
    }
}

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

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

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

4 个答案:

答案 0 :(得分:4)

尝试这一点,if条件以及where条件存在问题。并且我们无法回显对象或无法将对象转换为字符串。

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");
    }
}
?>

答案 1 :(得分:0)

将构造函数的名称从__dao()更改为__construct()。

将您的第6行代码替换为:

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

答案 2 :(得分:0)

试试这个:

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;
        }
    }

答案 3 :(得分:0)

连接文件:

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

dao文件:

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;
    }
}

但我认为最好使用PDO或更好的ORM系统,比如laravel eloquent。