php – using JOIN in mysqli only return result from one table-ThrowExceptions

Exception or error:

I’m currently trying to print the result of joining 2 tables using mysqli. but everytime i try to do it, it only return the result from one table while the table that it’s joining doesn’t return any result

below is the code that show the data into a table

    foreach($db->showdata() as $x) {
    					
          if($x['role_id'] == 1 || $x['role_id'] == 2) continue;
          $id = $x['user_id'];
          echo "<tr>";
          echo "<td>" . $x['user_id'] . "</td>";
          echo "<td>" . $x['first_name'] ." ". $x['last_name']. "</td>";
          echo "<td>" . $x['nilai_tugas'] . "</td>";
          echo "<td>" . $x['nilai_uts'] . "</td>";
          echo "<td>" . $x['nilai_uas'] . "</td>";
          echo "</tr>";
                    }

while this one is the showdata() function it called

function showdata(){
$data = mysqli_query($this->db, "SELECT * FROM user LEFT OUTER JOIN grade ON(user.user_id = grade.user_id) IS NOT NULL;");
while($d = mysqli_fetch_array($data)){
  $hasil[] = $d;
}
return $hasil;
}

The structure of the table is

User: user_id, first_name, last_name and other column that’s irrelevant for this part

Grade: user_id, nilai_tugas, nilai_uts, nilai_uas

when I run the code it return

Notice: Undefined index: nilai_tugas
Notice: Undefined index: nilai_uts
Notice: Undefined index: nilai_uas

which is from the grade table, but the data from the user table do go through and show up in the page.
The page result

When I run the query in mysql it does return all the data I requested but it doesn’t seem to return the data when in the function.

Thank you in advance.

Edit: I tried to move the select query into the same page and it work, it just don’t work when I use return from function(which is mandatory).

How to solve:

Can you try be more explicit in selecting your columns?

mysqli_query($this->db, "SELECT user.columnname, grade.columnname FROM user LEFT OUTER JOIN grade ON(user.user_id = grade.user_id) IS NOT NULL;");

And are you sure the column in users is called user_id? Because the result set has the columnname studentID.

Answer´╝Ü

I don’t fully know what happened, so I tried reinstalling xampp and it finally start working, the problem might be located in mysql config file(not the first time this happen to me).

Leave a Reply

Your email address will not be published. Required fields are marked *