0

I'm new to the community and would love a push into the right direction. Just started working with php for a few days and I ran into something I just can't get right. Could you guys help me out?

<?php
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }
  $sql = "SELECT * FROM users";
  $result = $conn->query($sql);
  if (!$result) {
      die("Invalid query: " . $conn->error);
  }
  while($row = $result->fetch_assoc())  {
      
      echo "<tr>
      <td>" . $row["role"] . "</td>
      </tr>";
  }

?>

This is an example code of what I'm working on. This code works, I have a separate file for the connection that's included in my code. I'm displaying all the rows from the database table "users" with the contents of "role". That works, but that's not what want to show.

This is what I'm trying to do and I can't get my head around it. I want to compare the value of "role" with the value of "role" in a different table: "roles". If it matches, I want to show the value of "rolename" out of the "roles" table out of the row that matches the "role" from the "users" table. Does that make sense?

Thanks to whomever can help me out! :)

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 3
    An inner join will get the data you want. Please include a few rows of sample data and the expected result. – The Impaler Jul 27 '22 at 13:29
  • 1
    Consider using a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) to solve problems like this. With those you have patterns you could follow for organizing your code into proper model, view and controller contexts. What you have here is a confused stew of concerns, with HTML, PHP, and SQL all jumbled together. Frameworks come in many forms from really lean like [Fat-Free Framework](https://fatfreeframework.com/) to exceptionally full-featured like [Laravel](http://laravel.com/) and many spots in between. – tadman Jul 27 '22 at 13:33
  • @TheImpaler thanks! With inner join i got it to work! – Hendrikus Jul 27 '22 at 17:27

0 Answers0