How to perform recursive queries? The name of my database table is aaa and the content is as follows. How do I recursively query the parent node?
USER PUSER
16
24 16
19 16
21 24
32 19
For example, if you check 32: is the row where 32 19 and 16 are located? That's what I wrote. What should I do?
$ Result = mysql_query ("select * from 'AAA' where user = '32' union all
Select h. *, h1. from 'AAA' h join result h1 on h. user = h1.Puser ", $ link );
$ Row = mysql_fetch_row ($ result );
Reply to discussion (solution)
You all know that recursive queries are required. Why didn't you do it?
Generally
function foo($id) { $res = array(); $rs = mysql_query("select * from aaa where user='$id'"); if($row = mysql_fetch_assoc($rs)) { $res[] = $row; $res = array_merge($res, foo($row['puser'])); } return $res;}
Search until puser is empty
$ids = array();function getuser($user){ global $ids; $result=mysql_query("select * from `aaa` where user='".$user."'",$link); $data=mysql_fetch_assoc($result); if($data['puser']!=''){ $ids[] = $user; getuser($data['puser']); }}getuser(32);print_r($ids);
Thanks to the two great gods, we still need to use the function. I will study it. thank you to the two great gods.
Right. recursion means that the function calls itself.
Thank you!