简体   繁体   English

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

[英]PHP database query not completing in time?

I have a php function that on page load checks the database to see if a session exists for the user that may be resuming a session. 我有一个php函数,可以在页面加载时检查数据库,以查看是否存在可能正在恢复会话的用户会话。 If nothing exists in the database, we start them on "Step 1" of the process. 如果数据库中不存在任何内容,则在流程的“步骤1”上启动它们。 However, if they do have an active session they are returning to, we get the current step number they left off on. 但是,如果他们确实要返回活动会话,则我们将保留当前的步骤号。 Here is my code: 这是我的代码:

<?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 you look inside this statement 如果你看这句话

 if($retrieveSession->sessionData){

The $currentStep variable seems to not be getting set before other parts of the code below this have executed. 在执行下面的代码的其他部分之前,似乎没有设置$ currentStep变量。

Is there something obvious I'm missing? 有什么明显的我想念的东西吗?

Is this as simple as you having the If Else Endif terminator in the wrong place?? 这和在错误的地方放置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();    

?>

If thats it, then proper indentation of code may help you identify issues in the future. 如果是这样,那么适当的代码缩进可能会帮助您将来确定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM