0

我有个问题。我应该在 php 的 foreach 循环中更改变量的值,但该值始终保持预定义的值。这是代码:

$returnValue = "";
foreach($vinArray as $vinValue){
    $sql_vin_check = "SELECT * FROM users WHERE vin LIKE '%:vin%'";
    $stmtvincheck  = $pdo->prepare($sql_vin_check);
    $stmtvincheck->bindParam(':vin', $vinValue);
    $stmtvincheck->execute();
    $vinCheck = $stmtvincheck->rowCount();
    $stmtvincheck->closeCursor();
    echo $vinValue;
    if($vinCheck != 0){
        $returnValue = $vinValue; break;
    }
}
return $returnValue;

$returnValue持有""价值。我尝试回$vinValue显变量,循环中断时它不是空的。

我究竟做错了什么?

4

1 回答 1

0

试试这个:

$returnValue = "";
foreach($vinArray as $k=>$vinValue){
    $sql_vin_check = "SELECT count(*) c FROM users WHERE vin LIKE '%:vin%'";
    $stmtvincheck  = $pdo->prepare($sql_vin_check);
    $stmtvincheck->bindParam(':vin', $vinValue);
    $stmtvincheck->execute();
    $vinCheck = $stmtvincheck->fetch();
    $stmtvincheck->closeCursor();
    if($vinCheck['c'] > 0){
        $returnValue = $vinValue; 
        break;
    }
}
return $returnValue;

如果没有其他预期,我还建议您将 returnvalue/break 替换为从那里直接返回(此建议与您的问题无关)

于 2013-09-19T15:36:24.363 回答