【问题标题】:How to connect to database using mvc in php [duplicate]如何在php中使用mvc连接到数据库[重复]
【发布时间】:2016-05-03 13:16:46
【问题描述】:

dbconnect.php

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

dao.php

include 'dbconnect.php';
class dao extends dbconnect
{
    private $conn;
    function __construct()
    {
        $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;
    }
}

controller.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';
    }
}

我遇到了一个错误

  • 警告: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

请帮我解决这个问题。

【问题讨论】:

  • $this->conn !== $conn
  • 主要问题是您没有在构造函数中创建类属性$conn,而只是一个局部变量。另外,你应该使用__construct(),而不是 php4 风格的构造函数(实际上是一种奇怪的混合)
  • 那我该如何解决呢?请告诉我我是 php 的新开发人员

标签: php mysqli


【解决方案1】:

试试这个,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");
    }
}
?>

【讨论】:

    【解决方案2】:

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

    将第 6 行代码替换为:

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

    【讨论】:

    • 我正在替换它,但也给了我同样的警告。
    • 1.添加额外的检查: if (!$this->conn) throw new Exception('Can`t connect to database.');在 $this->conn = $dbcon->connect(); 之后2. 如何以及在哪里创建新的 dao 实例?
    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:

      连接文件:

      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 会更好,或者像 laravel eloquent 这样的 ORM 系统会更好。

      【讨论】:

      • 好的,现在可以了,但是再给我一个错误你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在第 1 行的 '' 附近使用的正确语法
      • $table、$where、$other 的参数是什么?
      • 包括'dao.php'; $d = new dao(); if(isset($_POST['btn_login'])){ 提取($_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();标头(“位置:index.php”); } else{ $_SESSION['error'] = '无效的用户名或密码'; } }
      • 它保存表的名称 where variable 包含 where 条件,other 保存其他条件
      • puh,... ehm.. 首先你不应该使用 extract ... 而且你不需要它,因为你使用 $_POST[...] 的方式是正确的.. . 接下来,在将用户输入直接插入 sql 语句之前,您应该始终检查用户输入。我认为错误是因为你忘记了最后的“;”和 sql 语句中的“WHERE”关键字。
      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 2020-04-15
      • 2016-07-30
      • 2018-10-28
      • 2018-09-30
      • 2021-11-11
      • 2014-06-15
      相关资源
      最近更新 更多