Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我的函数不返回返回值?_Php_Mysql_Pdo - Fatal编程技术网

Php 为什么我的函数不返回返回值?

Php 为什么我的函数不返回返回值?,php,mysql,pdo,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()

好的,这是我的函数:

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;
        }
    }
}
}
代码执行得非常完美,从数据库中提取值并进行比较,然后按照我希望的方式执行所有操作。它在我可以测试的每种情况下都能得到正确的值,但它完全拒绝将该值返回给调用脚本。有人看到我掩饰过的愚蠢错误吗?在return语句之前立即使用debug echo似乎可以消除在return语句之前出错的可能性,但我现在还不知道

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

当您打电话时

getNewJobNumber($jobPrefix,$addition+1)

您实际上并没有返回值

换成


返回getNewJobNumber($jobPrefix,$addition+1)

您正在递归调用函数,但没有对返回值执行任何操作:

    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)另一个小提示:在开始时递增addition变量,然后传递(再次递增)对self的值,看起来可能是个错误。谢谢,这确实是个问题,我现在也明白为什么它有意义了。关于双增量,你也是对的。为了测试它可以跳过多个增量,我将数据库设置为跳过两个,但没有意识到它同时跳过了两个。这是一个错误lso现在也修复了,非常感谢您的帮助。谢谢,这个解决方案是绝对正确的。只是因为更详细的信息而检查了另一个人,同时也指出了我的其他问题。非常感谢您的回复,我非常感谢。
return:nextJobNumber-3005
:}{:
    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?
    }