在数组上调用成员函数fetch_assoc() [英] Call to a member function fetch_assoc() on array

查看:75
本文介绍了在数组上调用成员函数fetch_assoc()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发测验应用程序,但出现此错误:

I'm developing a quiz app and I'm this error:

致命错误:未捕获错误:在C:\ wamp64 \

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on array in C:\wamp64\

怎么了?

/**
* Get the Question
*/
$query = "SELECT * FROM questions
WHERE question_number = $number";

//Get result
$result = mysqli_query ($conn,$query);

$question = $result->fetch_assoc();


/*
* Get choices
*/
$query = "SELECT * FROM choices
WHERE question_number = $number";

//Get result
$result = mysqli_query ($conn,$query);

$choices = $result->fetch_assoc();
?>

<html>
<body>
    <main>
<div class="container" >
<div class="current">Question 1 of 5</div>
<p class="question">
 <?php echo $question ['text']; ?>
</p>

  <?php while ($row = $choices->fetch_assoc()) : ?>
  <li><input name="choice" type="radio value="<?php echo $row ['id'];?> " /><?php echo $row ['text']; ?></li>
      <input type="submit" value="Sumbit your answer"/>
  </ul>


 </main>

    </body>
</html>

<?php endwhile; 

推荐答案

您正在使用此行将一行插入到PHP数组中:

You are fetching a single row into PHP array with this line:

$choices = $result->fetch_assoc();

然后在while循环中使用该数组:

Then you use that array in your while loop:

while ($row = $choices->fetch_assoc())

您不能在数组上调用fetch_assoc()

您应该做的是将所有行都提取到多维数组中,然后在该数组上foreach.

What you should have done is fetched all rows into a multi-dimensional array and then foreach on that array.

$result = mysqli_query($conn, $query);
$choices = $result->fetch_all(MYSQLI_ASSOC);

// and then loop:

foreach($choices as $row) :

$result是mysqli_object类的对象.直接使用此对象可能很困难.建议使用fetch_all()将所有记录提取到数组中.然后,您可以更改数组,过滤器,循环,访问特定的行等.您不能使用mysqli_result对象来做到这一点,并且逐行读取的方法可能会造成混淆.

$result is an object of mysqli_object class. Working directly with this object can be difficult. It is recommended to fetch all records into an array with fetch_all(). You can then change the array, filter, loop, access specific rows, etc. You can't do it with the mysqli_result object and the methods for reading row by row can be confusing.

但是您可以直接在mysqli_result上循环,这比while ($row = $choices->fetch_assoc())好得多.最大的优点是它将始终从头到尾循环播放,因此您可以循环播放很多次而无需后退.例如:

You can loop on mysqli_result directly though, which is much better than while ($row = $choices->fetch_assoc()). The biggest advantage is that it will always loop from the beginning to the end, so you can loop it many times without rewinding. For example:

$result = $conn->query('SELECT * FROM users LIMIT 3'); // 3 rows returned

var_dump($result->fetch_all(MYSQLI_ASSOC)); // <-- this works
var_dump($result->fetch_all(MYSQLI_ASSOC)); // <-- this will not work without rewiding

// both of the loops will work
foreach ($result as $row) {
    var_dump($row);
}
foreach ($result as $row) {
    var_dump($row);
}

foreach循环也更简洁,更易于理解.您运行查询,然后循环搜索结果.

A foreach loop is also cleaner and easier to understand. You run a query and then you loop on the result.

当然,mysqli_result对象不是数组.您可以在其上循环,但不能通过数组索引访问特定的行.这不会让您获得第一行:

Of course, mysqli_result object is not an array. You can loop on it, but you can't access specific rows via array index. This will not get you the first row:

mysqli_query($conn, '...')[0]; // <-- Uncaught Error: Cannot use object of type mysqli_result as array

如果您的SQL中有变量输入,则应使用准备好的语句.您已修复的查询应如下所示:

You should be using prepared statements if you have variable input in your SQL. Your queries fixed should look like this:

$stmt = $conn->prepare("SELECT * FROM choices WHERE question_number = ?");
$stmt->bind_param('i', $number);
$stmt->execute();
$result = $stmt->get_result();

foreach ($result as $row) {
    // echo HTML
}

这篇关于在数组上调用成员函数fetch_assoc()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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