php:递归函数/查询

时间:2016-03-24 10:03:53

标签: php mysql recursion

我有一张桌子:

officer|user
------------
25     |22
25     |3
25     |16
16     |21
16     |13

当我以军官25的身份登录时,需要列出所有用户的日常记录(22,3,16,21,13),将来可能会有千或百万条记录。
这是我的递归函数:

function getRootUser($currentUser,&$array,$count){
   $getRootSQL=base_executeSQL("SELECT estaf_staff_user_id from estaf_staff where estaf_staff_officer=".$currentUser." AND estaf_staff_user_id!=0");

   if (base_num_rows($getRootSQL)!= 0)
   {
      while($rootdata_row = base_fetch_array($getRootSQL))
      {
         $currentUser=$rootdata_row["estaf_staff_user_id"];//1
         $array[]=$currentUser;
         $count = $count + 1;
         getRootUser($currentUser,$array,$count);   
      }
   }
   return $array;}

这是我的列表功能:

function eleav_listRequest($currentUser,$page, $record ,$search){

  getRootUser($currentUser,$array,0);

  $searchs = explode(" ", $search);

  $SQL =  "SELECT * FROM eleav_leave  " ;
  $countSearch = 0;

  foreach ($searchs as &$value) 
  {
    if ($countSearch == 0)
    {
        if (count($array)!=0)
        {
            $SQL .="WHERE (eleav_leave_user='".$array[0] ."' ";

            for($i=1;$i<count($array);$i++) 
                $SQL.="OR eleav_leave_user=".$array[$i]." ";
            $SQL.=") AND ";
        }
        else
            $SQL.="WHERE (eleav_leave_user='') AND ";

    }
    else
    {
        $SQL .=" AND ";
    }
    $SQL .=  "      (eleav_leave_code LIKE '%".$value."%' OR
                    eleav_leave_user IN (SELECT base_u_id FROM base_users WHERE base_u_name LIKE '%".$value."%') OR
                    eleav_leave_from_date LIKE '%".$value."%' OR
                    eleav_leave_to_date LIKE '%".$value."%' OR
                    eleav_leave_application_date LIKE '%".$value."%' OR
                    eleav_leave_status LIKE '%".$value."%') ";
        $countSearch ++;

}//....below continue with my print out data layout

目前它仍然可以工作,但我想我的递归功能可以在我的记录增加更多时存活下来。有时当我尝试加载数据时,它会带来错误的最大执行时间。
任何人都可以帮助改进或更好的递归吗?谢谢

0 个答案:

没有答案