0

我正在尝试创建一个脚本,我可以继续引用它来显示数据库条目。我相信我的问题是当我尝试显示结果时,我不知道如何调用 mysqli 和查询。我得到的错误是调用非对象上的成员函数 fetch_row()

class connectDatabase {


public $category;
public $query; 
public $mysqli;

    public function __construct($DB_HOST, $DB_USER, $DB_PWD, $DB_NAME)
    {
        $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PWD, $DB_NAME);
        return $this->mysqli = $mysqli;

    }

    public function queryInformation($category) {
        $query = "SELECT * FROM postad WHERE `category` = $this->category ORDER by date desc";
            return $this->query = $query;


    }
    public function displayResult() {
        $mysqli = $this->mysqli ; 
        $query = $this->query;
        $result = $mysqli->query($query);

        while ($row = $result->fetch_row()) {
        echo "$row[1] $row[2] $row[3] $row[4]  </br>" ;

    }

    }
}
4

2 回答 2

1

如果你 var_dump $result 它可能是布尔值 false,因为查询失败。如果成功,它应该是一个对象。如果返回值为布尔值,您需要有故障保护。

查看返回值: http: //php.net/manual/en/mysqli.query.php

于 2013-01-28T04:32:48.987 回答
0

我们可以稍微简化一下。

尝试这个

    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PWD, $DB_NAME);
    $category = "something";

    $result = $mysqli->query("SELECT * FROM postad WHERE `category` = '$category' ORDER by date desc");
    while ($row = $result->fetch_row()) {
        echo "$row[1] $row[2] $row[3] $row[4]  </br>" ;

    }
于 2013-01-28T04:17:25.963 回答