Skip to content
Advertisement

How do I get the column name of a foreign key reference in PHP

I have got two tables: category and quiz

In category I have : category_id | category_name |

In quiz I have : quiz_id | quiz_name | quiz_category_id| quiz_duration

As you can notice, quiz_category_id is a FOREIGN KEY reference.

This is the code I have so far:

get_all_exam() is a function and here it is: This is where I do the sql query

So at the minute, it just prints out for example;

|quiz name| |quiz category| |quiz duration|

|practice ex | |23| |5 minutes|

Instead of it saying 23 I want it so that it looks up on the corresponding table to get the name

Thanks

Advertisement

Answer

Inside select statement, select what you want, you could also say SELECT quiz.*, category.*, but be careful if you have same field names, you might want to retrieve some with AS.

LEFT JOIN means – always show from left table, even if there is no value in the right table; thus, it will show quiz_id, even if there is no corresponding category_id. RIGHT JOIN will do just the opposite.

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