如何将我的connection.php连接到我的模型

时间:2019-10-11 06:56:45

标签: php mysqli

当我尝试使用fetch_assoc获取数据时,它说我的连接为空 请帮我解决这个问题。

Connection.php

class Connection{

    public $servername;
    public $username;
    public $password;
    public $dbname;
    public $conn;

    public function connect(){
        $this->servername = "localhost";
        $this->username = "root";
        $this->password = "";
        $this->dbname = "crud";
        $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);

        return $this->conn;
    }
}

当我使用getAll()方法时,它无法显示数据库中的数据。 当我尝试使用var_dump($result)方法中的getAll()时,显示bool(false)

MyModel.php

<?php
include "connection.php";

class MyModel extends Connection{

    public $condition;
    public $table;
    public $query;
    public $select;
    public $join;
    public $insert;
    public $update;
    public $delete;
    public $conn;

    public function __construct(){
        $this->conn = $this->connect();
        $this->condition = '';
        $this->table = '';
        $this->select = '*';
        $this->join = '';
        $this->insert = '';
        $this->update = '';
        $this->delete = '';
    }

    public function table($table){
       $this->table = $table;
       return $this;
    }

    public function select(...$select){
        $this->select = implode(',',$select);
        return $this;
    }

    public function getAll(){
        $sql = "SELECT " . $this->select . " FROM " . "'" . $this->table . "'";
        $result = mysqli_query($this->conn, $sql);

        // for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row);
        // var_dump($set);

        return $result;
        //var_dump($result);
    }
}

    $model = new MyModel;
    $model->table('products')->getAll();

1 个答案:

答案 0 :(得分:-1)

问题是您没有检查任何错误!

始终将->error与您执行的任何查询结合使用。

您甚至可以将其简单地添加到同一行:

$result = mysqli_query($this->conn, $sql) or die($this->conn->error);
                                        //  ^^^^^^^^^^^^^^^^^^^^^^^^

有问题的行是实际查询:

$sql = "SELECT " . $this->select . " FROM " . "'" . $this->table . "'";
                                            // ^                    ^

如果您希望表名或列名带有引号,请使用反引号 `。因此错误的返回值是错误的。

修复该问题后,在执行查询后,请通过fetching返回行。

在这种情况下,您可以使用->fetch_all()

所以要把所有东西缝合起来:

public function getAll()
{
    $data = [];
    $sql = "SELECT {$this->select} FROM {$this->table}";
    $result = mysqli_query($this->conn, $sql) or die($this->conn->error);
    $data = $result->fetch_all(MYSQLI_ASSOC);

    return $data; 
}

旁注:不过,您需要注意,如果不将表名和列名列入白名单,查询中可能会发生任何事情。

另一个注意事项:由于您处于开发模式,因此要处理查询错误,请在实例化连接对象后设置错误报告:

public function connect()
{
        $this->servername = "localhost";
        $this->username = "root";
        $this->password = "";
        $this->dbname = "test";
        $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
        // add this
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

        return $this->conn;
    }
}

因此,您可以依次使用try和catch来执行查询。然后,您将知道查询失败的原因。

public function getAll()
{
    $data = [];
    $sql = "SELECT {$this->select} FROM {$this->table}";
    try {
        $result = mysqli_query($this->conn, $sql);
        $data = $result->fetch_all(MYSQLI_ASSOC);
    } catch (Exception $e) {
        echo "SQL: [{$sql}] " . $e->getMessage();
    }

    return $data; 
}