Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hate doing <code>SELECT *</code> because of their maintenance problems, but to work with what you did, just do a <code>JOIN</code>:</p> <pre><code>"SELECT * FROM Cat LEFT OUTER JOIN SubCat ON SubCat.under_cat = Cat.cat_id;" </code></pre> <p>This will return the result set you want, but you will need to modify your PHP in response to create the list appropriately, something like this:</p> <pre><code>if($row['cat_id'] === $id){ echo '&lt;li&gt;&lt;a href="#"&gt;'.$row2['subcat_name'].'&lt;/a&gt;&lt;/li&gt;'; } else { echo '&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;'.$row['cat_name'].'&lt;/a&gt;&lt;ul&gt;'; iF(!empty($row['subcat_name'])){ echo '&lt;li&gt;&lt;a href="#"&gt;'.$row['subcat_name'].'&lt;/a&gt;&lt;/li&gt;'; } } $id = $row['cat_id']; </code></pre> <p>This will add the items to the list if it is the same as the previous entry, else it will start a new list. The <code>!empty</code> thing is a quick attempt at handling NULLs, as in where there were no matches brought back from <code>SubCat</code> but you had entries in <code>Cat</code>. If this would never be the case, you can eliminate the <code>if</code> statement and convert your query to this:</p> <pre><code>"SELECT * FROM Cat INNER JOIN SubCat ON SubCat.under_cat = Cat.cat_id;" </code></pre> <p>Why go through all this effort you ask? Because this will provide the result set you want while only querying <strong>once</strong>. The way you had it structured prior would query a bazillion times, which kills the server and greatly reduces performance.</p> <p>Hopefully this helps both your problem and you're overall implementation knowledge.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload