我的資料庫表名為aaa,內容如下。我如何遞迴查詢父節點?
USER PUSER
16
24 16
19 16
21 24
32 19
如查32:則查出:32 19和16所在行?我是這麼寫的,好像不行啊。應該怎麼寫?
$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);
回複討論(解決方案)
你都知道要遞迴查詢了,那你怎麼沒去做呢?
大致這樣
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;}
一直搜尋,直到puser為空白
$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);
謝謝兩位大神,這還是要用function的啊,我研究學習一下,謝謝兩位大神。
對呀,遞迴就是函數自己調用自己
謝謝兩位大神!