doujiang1939 2016-05-03 13:16
浏览 80
已采纳

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

  • 写回答

4条回答 默认 最新

  • dongxianrang9269 2016-05-03 14:14
    关注

    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");
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里