【问题标题】:Use method mysqli in php oop [duplicate]在php oop中使用方法mysqli [重复]
【发布时间】:2016-05-09 14:15:44
【问题描述】:

我有一个问题:为什么我不能在我的脚本中使用方法 fetch_object()。我有一个方法:

public function magazyn() {
    //return 'magazyn';
    $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `nazwa`");
    //$this->magazyn->bind_param('ssss', $id, $nazwa, $kategoria, $sn);
    $this->magazyn->execute();
    $this->magazyn->store_result();
    //return $this->magazyn;

    if ($this->magazyn->num_rows > 0) {

        $rows = array();
        while ($row = $this->magazyn->fetch_object()) {
            echo $rows[] = $row;

        }

    } else {
        echo "Brak produktw w bazie";
    }
}

脚本返回错误:

致命错误:未捕获的错误:调用未定义的方法 mysqli_stmt::fetch_object() in

但我可以使用例如“准备”:

$this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `nazwa`");

【问题讨论】:

  • $poop = $this->magazyn->store_result(); 然后while ($row = $poop->fetch_object())
  • 基本上是因为mysqli_stmt对象没有一个名为fetch_object的方法

标签: php oop mysqli fetch


【解决方案1】:

您不能直接在语句对象上使用fetch_object。正如这里所说:

Is it possible to use mysqli_fetch_object with a prepared statement

在文档中 http://php.net/manual/en/mysqli.prepare.php

您应该获得结果并将 fetch_object 应用于该结果。

/* execute query */
$stmt->execute();

$result = $stmt->get_result();

/* now you can fetch the results into an object */
while ($myrow = $result->fetch_object()) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2013-11-16
    • 2015-05-05
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多