title: mysql select recursive query
body: <p>I have this table:</p>
  <p><img src="https://i.stack.imgur.com/2QsJM.png" alt="table with records example" /></p>
  <p>I am trying show a hierarchical combo something like this:</p>
  <p><img src="https://i.stack.imgur.com/zKrO8.png" alt="combo" /></p>
  <p>It is posible to do this using a select statement or how I can implement this via PHP? I was googling and I found that it is posible using recursive functions but it is new for me and I can not implement this. May be somebody give me an idea?</p>
  <p><strong>Update:-</strong> I finally solved my problem. I create a recursive function based on @user2433317 code, first I create and initialized a variable to control the amount of blank spaces that I need in each level</p>
  <pre><code>$n=-3;
  </code></pre>
  <p>next I write the recursive function</p>
  <pre><code>function ShowSubCats($id){
  global $cn, $n;
  $sql = &quot;SELECT * FROM menus WHERE idPadre = '$id';&quot;;
  $r = mysqli_query($cn, $sql);       
  if (mysqli_num_rows($r) &gt; 0) {
      $n+=3;
      while ($row = mysqli_fetch_object($r)) {
          echo &quot;&lt;option value='$row-&gt;id'&gt;&quot;.space($n).$row-&gt;titulo.&quot;&lt;/option&gt;&quot; ;
          ShowSubCats($row-&gt;id);                  
      }
          $n-=3;
      }
  }
          
  </code></pre>
  <p>and call it with 0 argument that mean idPadre=0 don't have parent.
  Finally this is the space function</p>
  <pre><code>function space($n){
  $str = &quot;&quot;;
  for ($i=0; $i&lt;$n; $i++) { 
      $str .= &quot;&amp;nbsp;&quot;;
  }
  return $str;
  </code></pre>
  <p>}</p>
  <p>and the result is showed here</p>
  <p><img src="https://i.stack.imgur.com/lHArr.png" alt="enter image description here" /></p>
  <p>thank you for your answers and sorry for my english is not very good</p>