为什么我的函数没有返回返回值?

时间:2013-01-14 17:58:28

标签: php mysql pdo

好的,这就是我的功能:

function getNewJobNumber($jobPrefix, $addition = "0") {
$addition = $addition + 1;
//echo $addition . "<br />";    
$yearDate = date("Y");
$firstDigit = $yearDate[strlen($yearDate) - 1];
$db = DatabaseHelpers::getDatabaseConnection();
$jobQuery = 'SELECT jobID, jobNumber, jobPrefix FROM tblJobNumbers WHERE jobPrefix = "' . $jobPrefix . '" AND jobNumber LIKE "' . $firstDigit . '___" ORDER BY jobID DESC LIMIT 1';
//echo $jobQuery . "<br />";
$stmt1 = $db->query($jobQuery);
$stmt1->setFetchMode(PDO::FETCH_OBJ);
$firstResult = $stmt1->fetch();
//above should select the latest created job number with selected prefix
//print_r($firstResult);
$jobNumber = $firstResult->jobNumber; //top row, will be last job number
//echo "jobNumberFromDB:" . $jobNumber . "<br />";
if (!$jobNumber) {
    //no job number exists yet, create one
    //will be last digit of year followed by "000" ie in 2013 first
    //new job number is "3000"
    $newJobNumber = str_pad($firstDigit, 4, "0");
    return $newJobNumber;
} else {
    //job number already exists, try next one
    $nextJobNumber = $jobNumber + $addition;
    $nextJobQuery = 'SELECT jobID, jobNumber, jobPrefix FROM tblJobNumbers WHERE jobPrefix = "' . $jobPrefix . '" AND jobNumber = "' . $nextJobNumber . '" ORDER BY jobID DESC LIMIT 1';
    $stmt2 = $db->query($nextJobQuery);
    $stmt2->setFetchMode(PDO::FETCH_OBJ);
    $nextResult = $stmt2->fetch();
    $dbNextJobNumber = $nextResult->jobNumber;      
    if (!$dbNextJobNumber) {
        //new job number is unique, return value
        echo "return:nextJobNumber-" . $nextJobNumber . "<br />";
        return($nextJobNumber);
    } else {
        //new job number is not unique, and therefore we need another one
        if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
            //in order to loop this programatically call function again, adding one to addition factor
            getNewJobNumber($jobPrefix, $addition+1);
        } else {
            return;
        }
    }
}
}

这是我的电话:

        $ourNewJobNumber = getNewJobNumber($_POST['txtJobPrefix'], 0);
        echo ":}" . $ourNewJobNumber . "{:<br />";

这是我的结果:

return:nextJobNumber-3005
:}{:

代码执行得非常完美,从数据库中提取值并比较它们并按照我想要的方式执行所有操作。它在我可以测试的每个环境中获得正确的值,但它完全拒绝将该值返回给调用脚本。有没有人看到我掩饰过的任何愚蠢的错误?在我的return语句之前立即调试echo似乎在返回语句之前消除了它出错的可能性,但我现在还不知道。

编辑:为了清楚起见,3005是我期待从我的数据库中获得的值。这是为了设置工作中始终为Zxxx的工作号,其中Z是一年中的最后一位数。这些总是按顺序创建,但是对于跨越一年以上的作业,我们只更改Z,因此这是我用来解决3030在创建3000之前可以(并且确实)存在的事实的代码。

2 个答案:

答案 0 :(得分:4)

当你打电话时

getNewJobNumber($jobPrefix, $addition+1);

你实际上并未返回该值。

将其更改为

return getNewJobNumber($jobPrefix, $addition+1);

答案 1 :(得分:4)

您正在以递归方式调用函数,但是您没有使用返回值执行任何操作:

    if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
        //in order to loop this programatically call function again, adding one to addition factor
        getNewJobNumber($jobPrefix, $addition+1);
    } else {
        return;
    }

应该是这样的:

    if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
        //in order to loop this programatically call function again, adding one to addition factor
        return getNewJobNumber($jobPrefix, $addition+1);
        ^^^^^^
    } else {
        return -1;    // some kind of error message?
    }