Skip to content
Advertisement

How to retrieve data from multiple tables in MySQL and PHP? [closed]

TLDR: Please point me to the SIMPLEST tutorial for my problem. I have read many tutorials but all they did was confuse me more.

I have three tables.

  1. students (has a row of id and name)
  2. courses (has a row of id and name)
  3. courses_students

The third table to link the two was created like this:

JavaScript

Then I add a simple data like this:

JavaScript

The 2 and 3 are ids in the courses and students tables, respectively. How do I retrieve the information for these ids (2 from the courses table, and 3 from the students tables) using mysqli+PHP statements?

Advertisement

Answer

It is pretty straight forward, there are many ways, here is the simplest one (assuming you’ve “established” a connection as described here https://www.php.net/pdo.connections):

JavaScript

That’s getting a bit more complex when you need to query with parameters – then you should read a bit more about bindings, but the general idea is like the following code:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement