1

I want to search in mysql table with specific column by using foreach with key value.

My code is:

$query= "select status from temp where id='1'";
$result=$oDbh->($query);
foreach($result AS $k => $v) {
   if($v['status']=='0' OR $v['status']=='1') {
      return true;
   }
   else {
      return false;
   }
}

In table temp status is 2,0,1,1,1,0.

If condition is searching only for first row and its returning false still there is 0 or 1 in table.

I am new here if there is more info needed than comment, please.

4

1 回答 1

0

我不确定你期望它做什么:

$query= "select status from temp where id='1'";
$result=$oDbh->($query);
foreach($result AS $k => $v) {
   if($v['status']=='0' OR $v['status']=='1') {
      return true;
   }
}

但是根据您的观察,我会说您需要continue在它们不匹配时利用它们。如果你return那么处理将停止 - 清楚地。除非我严重误解了您的观察,否则这将允许处理继续进行,直到您找到匹配项或完全退出循环。

此外,因为你没有问一个足够清楚的问题,所以几乎不可能确定这里发生了什么,但我要在这里走出困境并在循环之后立即说出来(即,如果你到那里没有找到匹配项),您将只想这样做:

return false;
于 2013-05-20T12:31:05.317 回答