在php中第二次调用后,递归函数不会执行

时间:2013-12-27 08:08:31

标签: php recursion

我正在开发一个自动化项目管理流程的Web应用程序。 我定义了一个task_node类,它代表项目中的任务。 completePath函数根据序列关系完成从当前任务$tNode到任务结束的项目路径。 我的问题是第一次调用completePath后执行停止了。 这是我的代码:

private function completePath($tNode,$counter)
{
    // getting data from DB works properly 
    echo '<br>counter:'.$counter.' <br>task name:'.$tNode->task_name;
    $helper=new MySQLHelper();
    //getting initial tasks 
    $cond=" t2.source_task_id='$tNode->task_id'
    And t2.relation_type='sequence'
            AND t1.task_id=t2.target_task_id";
    $tables="tasks t1 , tasks_relations t2";
    $nextTask=$helper->getData($tables,'t1.task_id,t1.task_name,t1.task_duration',$cond);
    if(mysql_num_rows($nextTask)!=0)  // stopping condition 
    {
        $row=mysql_fetch_array($nextTask);
        $tNode->nextTask=new Task_Node($row['task_id'],$row['task_name'],$row['task_duration']);
        completePath($tNode->nextTask,++$counter);//  execution stopped here
    }

}

任何想法?谢谢你

2 个答案:

答案 0 :(得分:0)

使用

sizeof($nextTask) 

count($nextTask) 

取代

mysql_num_rows($nextTask). 

因为mysql_num_rows()从结果集中检索行数。

答案 1 :(得分:0)

根据您提供的信息,您应该循环资源结果:

   if(mysql_num_rows($nextTask)>0)  // stopping condition 
        {
            while($row=mysql_fetch_array($nextTask,MYSQL_ASSOC)){ // loop through the result set 
            $tNode->nextTask=new Task_Node($row['task_id'],$row['task_name'],$row['task_duration']);
            completePath($tNode->nextTask,++$counter);//  execution stopped here
           }
        }

mysql_fetch_array返回一个与获取的行对应的数组,并将内部数据指针向前移动。

另请注意,mysql_ *函数已从PHP 5.5中弃用。请尝试使用mysqli

相关问题