How to perform recursive queries?

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.