我如何解决这个问题 在布尔值上调用成员函数 fetchAll()?



我想根据部门选择帖子表的所有数据 我尝试使用此代码,但出现此错误 在布尔值上调用成员函数 fetchAll(( 这是我的代码

<?php
if(isset($_GET['dept_n']))
{ 
$de_id = $_GET['dept_n'];
$sql = 'SELECT * FROM [posts WHERE dept_id = $de_id';
$result = $connection->query($sql);
$rows = $result->fetch_all();
foreach($rows as $row)
{
$id = $row[0];
echo"   <img src='./images/$row[3].jpg' style='width: 90%; height: 200px; '> 
<h4 >$row[1]</h4>
}}?>

首先,为什么你在这里有括号?

$sql = 'SELECT * FROM [posts WHERE dept_id = $de_id';

您不必使用foreach。使用 while 循环:

while ($row = $result->fetch_assoc()){
echo $row[“name of column”];
}

最新更新