我该如何解决此问题,请在布尔值上调用成员函数fetchAll()?

时间:2019-12-04 18:04:55

标签: php database

我想根据尝试使用此代码的部门选择职位表的所有数据,但出现此错误 在布尔值上调用成员函数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>
}}?>

1 个答案:

答案 0 :(得分:-1)

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

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

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

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