I am simply trying to output the data from database with while loop and using a prepared statement. The code looks as follows:
$result = $conn->prepare("SELECT * FROM students WHERE class=?");
$result->bind_param("s",$class);
if($result->execute()){
    $result->store_result();
    if($result->num_rows>0){
        $res = $result->get_result();
        while($row = $res->fetch_assoc()){  //line no. 73, error is here
            $rollno = $row['rollno'];
            $name = $row['name'];
            $image = $row['image'];
            $mobile = $row['mobile'];
            echo '<tr>
                    <td>'.$rollno.'</td>
                    <td>'.$name.'</td>
                    <td><img src="images/'.$image.'" class="img-thumbnail" width="8%" height="8%"></td>
                    <td>'.$mobile.'</td>
                    <td>
                        <a href="all_students.php?class='.$class.'&&rollno='.$rollno.'&&action=view"><i class="fa fa-eye" aria-hidden="true"></i></a>  
                        <a href="all_students.php?class='.$class.'&&rollno='.$rollno.'&&action=trash"><i class="fa fa-trash" aria-hidden="true"></i></a>
                    </td>
                </tr>';
        }
        $result->free();
    } else {
        echo '<tr><td colspan="5">No Records Found</td></tr>';
    }
    $result->close();
}
The above code is throwing error Fatal error: Call to a member function fetch_assoc() on boolean on line 73. I can run this successfully by using using query() function instead of prepared statement and with some slight modifications. But, the sql query then will be like "SELECT * FROM students WHERE class=".$class which has a risk of sql injection, thus I want to avoid it.
I referred the answer of similar question here which advised to add get_result() function. I already added it in above code, but that too didn't work for me. What could be the problem? How can I achieve it with prepared statement?
This is what I updated just now in above code:
$result = $conn->prepare("SELECT * FROM students WHERE class=?");
$result->bind_param("s",$class);
if($result->execute()){
    $result->get_result();
    if($result->num_rows){
        while($row = $result->fetch_assoc()){
            $rollno = $row['rollno'];
            $name = $row['name'];
            $image = $row['image'];
            $mobile = $row['mobile'];
            echo '<tr>
                    <td>'.$rollno.'</td>
                    <td>'.$name.'</td>
                    <td><img src="images/'.$image.'" class="img-thumbnail" width="8%" height="8%"></td>
                    <td>'.$mobile.'</td>
                    <td>
                        <a href="all_students.php?class='.$class.'&&rollno='.$rollno.'&&action=view"><i class="fa fa-eye" aria-hidden="true"></i></a>  
                        <a href="all_students.php?class='.$class.'&&rollno='.$rollno.'&&action=trash"><i class="fa fa-trash" aria-hidden="true"></i></a>
                    </td>
                </tr>';
        }
        $result->free();
    } else {
        echo '<tr><td colspan="5">No Records Found</td></tr>';
    }
    $result->close();
}
