gpt4 book ai didi

php:非对象错误 - 数组输出到表 - PHP 5.3 之前的准备语句

转载 作者:行者123 更新时间:2023-11-29 08:48:47 24 4
gpt4 key购买 nike

非对象错误错误:在非对象上调用成员函数 fetch_assoc()

我的代码:

 if($stmt = $dbc->prepare($query)) {
$stmt->bind_param('s', $search_term);
$stmt->execute();
$stmt->bind_result($bookid, $title, $author, $genre, $isbn, $url);

echo '<table><tr>
<th><a href="browse.php?sort=bi">ID</th>
<th><a href="browse.php?sort=t">Title</a></th>
<th><a href="browse.php?sort=a">Author</th>
<th><a href="browse.php?sort=g">Genre</th>
<th>ISBN</th>
<th>URL</th>
</tr>';


// array to hold all rows
$rowset = array();

// All results bound to output vars
while ($stmt->fetch()) {
// Append an array containing your result vars onto the rowset array
$rowset[] = array(
'bookid' => $bookid,
'title' => $title,
'author' => $author,
'genre' => $genre,
'isbn' => $isbn,
'url' => $url
);
} // End Fetch

$size = count($rowset);

for ($i=0; $i <$size; $i++){
$row = $rowset->fetch_assoc(); // Error is on this line

我对准备好的语句很陌生,但这部分正在发挥作用。我读: Paginate result set having written the query with prepared statements, .

因为我的服务器有 PHP 5.2.17,所以我无法使用 get_result()。所以我按照文章所述构建了一个数组。我可以 var_dump 它(因此查询有结果),但我想输出到表中。我尝试了不同的方法来输出查询,一切似乎都回到了这个非对象错误。在网上搜索时,我感到很困惑,因为 OOP 文章涉及的主题比我目前能做的更高级。所以我希望有人能指出我正确的方向。

预先感谢您提供的任何帮助。

最佳答案

错误消息说明了一切。 $rowset 不是一个对象,因此没有名为 fetch_assoc() 的成员函数。

此代码将 $rowset 设置为关联数组的数组。

$rowset = array(); 

// All results bound to output vars
while ($stmt->fetch()) {
// Append an array containing your result vars onto the rowset array
$rowset[] = array(
'bookid' => $bookid,
'title' => $title,
'author' => $author,
'genre' => $genre,
'isbn' => $isbn,
'url' => $url
);
} // End Fetch

fetch_assoc()mysqli_result 对象的成员函数。 ( Documenation here .)

由于您已经将数据存储在数组 ($rowset) 中,因此您可以使用 foreach 循环遍历每个索引:

foreach ($rowset as $row) {
// do something
}

关于php:非对象错误 - 数组输出到表 - PHP 5.3 之前的准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com