调用未定义的方法mysqli_result :: fetch() [英] Call to undefined method mysqli_result::fetch()

查看:150
本文介绍了调用未定义的方法mysqli_result :: fetch()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用fetch_assoc()fetch_all(),...和fetch_row()中的任何一种从get_result()中获取数据,但是当我尝试仅使用简单的fetch()时,我得到了错误

I am able to fetch data from get_result() using any of fetch_assoc(), fetch_all(), ..., and fetch_row() but when I am trying to use the simple fetch() only, I am getting this error

未捕获的错误:调用未定义的方法mysqli_result :: fetch()

Uncaught Error: Call to undefined method mysqli_result::fetch()

这是因为使用get_result()吗?还是我在以下代码中缺少了其他东西

Is this because of using get_result()? or am I missing something else in the following code

$stmt = $conn->prepare($SQL);
$stmt->bind_param("s", $date);
$stmt->execute();
$result = $stmt->get_result();
//$row = $result->fetch_assoc();
//$row = $result->fetch_all();
//$row = $result->fetch_row();
$row = $result->fetch();

推荐答案

变量$stmt fetch() 的方法,该方法返回一个布尔值(对/错).它仅与 bind_result() 结合使用

The variable $stmt is an object of the mysqli_stmt class. This class has a method called fetch() which returns a boolean (true/false). It is used only in conjunction with bind_result()

$stmt = $conn->prepare('SELECT myCol, myOtherCol FROM myTable WHERE dt=?');
$stmt->bind_param("s", $date);
$stmt->execute();
// The columns in SQL will be bound to the PHP variables
$stmt->bind_result($variable1, $variable2);
while ($stmt->fetch()) {
    // This will fetch each record from the prepared statement one by one
    printf ("myCol is %s and myOtherCol is %s\n", $variable1, $variable1);
}

$stmt->get_result() 返回一个对象class mysqli_result (顺便说一下,使用foreach).此类具有不同的方法,但没有fetch()方法.

$stmt->get_result() returns an object of class mysqli_result (which by the way is traversable using foreach). This class has different methods, but it doesn't have fetch() method.

  • fetch_all() 返回一个数组数组.顾名思义,它会立即返回结果集中的所有记录.
  • fetch_all() return an array of arrays. As the name suggests it returns all records from the result set at once.
$result = $stmt->get_result();
$allRecords = $result->fetch_all(\MYSQLI_ASSOC);
echo json_encode($allRecords);

  • fetch_array() 返回每条记录一个一维数组

  • fetch_array() returns each record one by one as an 1D array

    $row = $result->fetch_array();
    printf("%s (%s)\n", $row["myCol"], $row["myOtherCol"]);
    

  • fetch_assoc() 等效于fetch_array(\MYSQLI_ASSOC)
  • fetch_row() 等效于fetch_array(\MYSQLI_NUM)
  • fetch_object() 返回每条记录一个以一个作为对象.

  • fetch_assoc() is the equivalent to fetch_array(\MYSQLI_ASSOC)
  • fetch_row() is the equivalent to fetch_array(\MYSQLI_NUM)
  • fetch_object() returns each record one by one as an object.

    $row = $result->fetch_object();
    printf("%s (%s)\n", $row->myCol, $row->myOtherCol);
    

  • 但是,为简单起见,您可以直接在mysqli_result上循环,这将使您将每一行作为关联数组.

    However, to keep it simple you can just loop on the mysqli_result directly which will get you each row as an associative array.

    foreach($stmt->get_result() as $row) {
        printf("%s (%s)\n", $row["myCol"], $row["myOtherCol"]);
    }
    

    这篇关于调用未定义的方法mysqli_result :: fetch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆