PHP数据库查询未及时完成?

时间:2014-01-03 18:52:00

标签: php mysql

我有一个php函数,在页面加载时检查数据库以查看是否存在可能正在恢复会话的用户的会话。如果数据库中不存在任何内容,我们将在该过程的“步骤1”启动它们。但是,如果他们确实有一个活动的会话,他们将返回,我们得到他们停止的当前步骤编号。这是我的代码:

<?php   
//Start the session we will use to track the activity.
session_start();

//Get the session ID from a previous session if it exists; if not, create one.
$sessionID = $_GET['session'];
$sessionID = ($sessionID ? $sessionID : session_id());

//Define some vars
$mainPage = 'wizard';
require_once('includes/header.php'); 

//Lets see if we can retrieve an active session for the user.
$objDB = new DB;
    $retrieveSession = $objDB->setStoredProc('shadowFetchUserSession')
         -> setParam("sessionID", $sessionID)   
         -> setParam("empID", $empID)    
         -> execStoredProc()
         -> parseXML();

//Do we have a session already?
if($retrieveSession->sessionData){

            //This doesnt seem to get the variable in time before the rest of the code fires.
    $currentStep = $retrieveSession->sessionData->currentStep;

}else{  

//Since we didnt get a step number from the session, lets start at 1.
    $currentStep = 1;   

//Lets add the session to the database now that we have the sessionID and the current step.
$objDB = new DB;
    $makeSession = $objDB->setStoredProc('shadowCreateSession')
         -> setParam("sessionID", $sessionID)   
         -> setParam("empID", $empID)    
         -> setParam("currentStep", $currentStep)            
         -> execStoredProc();    
}
?>

如果您查看此声明

 if($retrieveSession->sessionData){

在执行此代码的其他部分之前,$ currentStep变量似乎没有设置。

有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:0)

这就像你将If Else Endif终结符放在错误的位置一样简单吗?

session_start();

//Get the session ID from a previous session if it exists; if not, create one.
$sessionID = $_GET['session'];
$sessionID = ($sessionID ? $sessionID : session_id());

//Define some vars
$mainPage = 'wizard';
require_once('includes/header.php'); 

//Lets see if we can retrieve an active session for the user.
$objDB = new DB;
$retrieveSession = $objDB->setStoredProc('shadowFetchUserSession')
                         -> setParam("sessionID", $sessionID)   
                         -> setParam("empID", $empID)    
                         -> execStoredProc()
                         -> parseXML();

//Do we have a session already?
if($retrieveSession->sessionData){

    //This doesnt seem to get the variable in time before the rest of the code fires.
    $currentStep = $retrieveSession->sessionData->currentStep;
} else {  

    //Since we didnt get a step number from the session, lets start at 1.
    $currentStep = 1;   
}

//Lets add the session to the database now that we have the sessionID and the current step.
$objDB = new DB;
$makeSession = $objDB->setStoredProc('shadowCreateSession')
                     -> setParam("sessionID", $sessionID)   
                     -> setParam("empID", $empID)    
                     -> setParam("currentStep", $currentStep)            
                     -> execStoredProc();    

?>

如果是这样,那么适当的代码缩进可以帮助您确定将来的问题。