0

I am trying to list the students according to their class, when I select the class name and refresh the table, it is not listing the students and gives a fatal error

Fatal: Call to a member function fetch_assoc() on boolean in C:\xampp...

This is the function I have coded and with this I want to select students according to their class:

    function std_list_header(){

    $Connection=new mysqli("localhost","root","","dar_ul_hidaya");

    $query =" SELECT * FROM class";


        $res = mysqli_query($Connection, $query);

        if($res){
            $rs=mysqli_fetch_assoc($res);




            echo '<tr><td colspan="2" style="border-right:none;"><b>Class:'.$rs["jamat"].'</b></td>



            <td colspan="3" >
            <form action="student.php" method="POST">
            <select name="jamat">

<option value="-1">Select Class</option>';
$jamat=""; 
if (isset($_POST["jamat"])) {
    if($_POST["jamat"]>=0){
        $jamat=$_POST["jamat"]; 
    }
}


    $query="SELECT jamat FROM class";
    //echo $query; 
    $result=$Connection->query($query); 


    while($rs=$result->fetch_assoc()){

            echo"<option value=".$rs["id"]." ".($jamat==$rs["id"]?"selected":"").">".$rs["jamat"]."</option>";
    }
    echo '</select>
    <input type="submit" value="Refresh">
    </form>
                </td>


                </tr>';}


    }

And this is the main page where I display the list of students:

<table align="center"  border="1px" width="600px">


<?php
        std_list_header();

?>



<tr>
    <th width="30px">کلاس</th>
    <th width="30px">ولدیت</th>
    <th width="30px">نام</th>
    <th width="30px">نمبر شمار</th>
</tr>



<?php

    $query="SELECT student.id ,student.name ,student.fname,student.class FROM  
student
inner join enroll on student.id=enroll.student_id 
inner join class on enroll.class_id=class.id"; 

if (isset($_POST["jamat"])) {
    if($_POST["jamat"]>=0){
$query=$query." where class.id=".$_POST["jamat"]; 

    }; 

}



$resultset=$Connection->query($query); 
$count=0; 
while($r=$resultset->fetch_assoc()){

    $count++; 
echo "

            <tr>

            <td>".$r["class"]."</td>
            <td>".$r["fname"]."</td>
            <td>".$r["name"]."</td>
            <td>".$count."</td>
            </tr>
"; 
}

?>



    </table>
</tr>
</table>
hassan
  • 7,812
  • 2
  • 25
  • 36
  • Shouldn't `$rs` be `$res`? – Mitya Aug 27 '17 at 10:13
  • no this one is ok because $res = mysqli_query($Connection, $query); if($res){ $rs=mysqli_fetch_assoc($res); – M Younas Muntazir Aug 27 '17 at 10:15
  • 1
    `$Connection->query()` returns false when you have an error in your qeury (e.g. non existing table name etc). Check this, and execute `var_dump($Connection->error)` to get more information when this happens. By the way: choose either one of the MySQLi-flavors, object oriented or procedural - don't mix them up – Peter van der Wal Aug 27 '17 at 10:16

0 Answers0