I have two tables namely 'categories' & 'products' as follows:
categories
id | name
products
id | cat_id | name
cat_id in products references the categories table.
Now, I want to display the list of all products on a page, which will have to also display the name of the category the product belongs to(not the cat_id). I am very new in PHP, so am doing the following way:
foreach($products as $product):
echo $product->id . $product->name;
echo getCategoryName($product->cat_id);
endforeach;
Its obvious that the database call to retrieve the corresponding category name is equal to the number of products to be displayed, and something tells me this is not good at all.
So, is there a better way to do the above? maybe by using SQL JOIN?