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

如何将my connection.php连接到我的模型,php,mysqli,Php,Mysqli,当我尝试使用fetch_assoc获取数据时,它会说我的连接为空 请帮我解决这个问题 Connection.php class Connection{ public $servername; public $username; public $password; public $dbname; public $conn; public function connect(){ $this->servername = "loc

当我尝试使用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()
方法时,它无法显示数据库中的数据。 当我在
getAll()
方法中尝试
var\u dump($result)
时,它会显示
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();

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

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

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

$result = mysqli_query($this->conn, $sql) or die($this->conn->error);
                                        //  ^^^^^^^^^^^^^^^^^^^^^^^^
问题行是实际查询:

$sql = "SELECT " . $this->select . " FROM " . "'" . $this->table . "'";
                                            // ^                    ^
如果希望表或列名带有引号,请使用倒勾
`
。因此,错误返回值为false

一旦解决了这个问题,在执行查询后,通过
获取返回行

在这种情况下,您可以使用
->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-and-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; 
}

@你的常识我不知道这一点,谢谢你链接出来,我不是mysqli的粉丝,所以我从来都不用费心使用它。谢谢@Kevin在错误报告中提到了很多。@StephenPascual我想请你阅读我现在删除的评论中链接到的内容。这并不重要,但我相信我提出了一种革命性的方法来处理PHP中的错误,您可能会觉得有点有趣。请不要使用这个类。有这么多的问题,我甚至不知道从哪里开始。它在每个方法中都有SQL注入漏洞。请先学习mysqli编写的语句。这是您使用原始mysqli的代码<代码>包括“mysqli.php”$mysqli->query('SELECT*FROM products')->fetch_all()请参见,它与您自己的代码一样简单,但没有缺点。