php - Mysql Selects in nested loops -


i have table named people:

id | name |parent_id ---+------+--------- 1  | john | 0 2  | jane | 1 3  | james| 1 4  | jack | 0 5  | jim  | 4 6  | jenny| 4 

so john parent of jane , james. tree goes this.

john -jane -james jack -jim -jenny 

i want make table seems like

<table border="1">     <tr>         <th colspan="2">john</th>     </tr>     <tr>         <td>-</td><td>jane</td>     </tr>     <tr>         <td>-</td><td>james</td>     </tr>     <tr>         <th colspan="2">jack</th>     </tr>     <tr>         <td>-</td><td>jim</td>     </tr>     <tr>         <td>-</td><td>jenny</td>     </tr> <table> 

to this, use 2 sql queries. here pseudo-code:

<?php  $firstquery = 'select id, name people parent_id = 0';  start creating table  while ($rowp = $result_parent->fetch()) {     //get child rows using second query in loop:      $secondquery = 'select id, name people parent_id = $rowp["id"]';      start creating table rows child items.      while ($rowc = $result_child->fetch())     {         add names table belonging current parent person     } }  ?> 

so problem rises here.

  1. this bad approach in performance asppect. correct way.

  2. when try use parent person's id parameter child people query, error bind_param() function.

  3. this can done 1 sql query join operation. don't know how do.

this bad approach in performance asppect. correct way.

it not.
few primary key lookups never harm.

when try use parent person's id parameter child people query, error bind_param() function.

first of all, have not mention error message read , comprehend it, provide here, full , uncut.

next, 1 quite easy guess though. use store_result()

this can done 1 sql query join operation. don't know how do.

a canonical text has been part of mysql official docs once: managing hierarchical data in mysql (first result on google, btw)


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -