title: PHP how to display rows from two tables
body: <p>I have two tables namely 'categories' &amp; 'products' as follows:</p>
  
  <pre><code>categories
  id | name
  
  products
  id | cat_id | name
  </code></pre>
  
  <p>cat_id in products references the categories table.</p>
  
  <p>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:</p>
  
  <pre><code>   foreach($products as $product):
         echo $product-&gt;id . $product-&gt;name;
         echo getCategoryName($product-&gt;cat_id);
     endforeach;
  </code></pre>
  
  <p>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.</p>
  
  <p>So, is there a better way to do the above? maybe by using SQL JOIN?</p>