-2

I want to select all data for posts table according to department I tried to use this code but I got this error Call to a member function fetchAll() on boolean this is my code

    <?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>
}}?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

-1

First of all why you have bracket here?

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

You dont have to use foreach. Use while loop:

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