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 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统