dongpu3792 2013-05-20 12:28
浏览 24
已采纳

如果条件只搜索第一行

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.

  • 写回答

1条回答 默认 最新

  • douao7937 2013-05-20 12:31
    关注

    I'm not sure what you expected it to do:

    $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;
       }
    }
    

    but based on your observation I would say you need to leverage the continue when they don't match. If you return then processing will stop -clearly. Unless I've grossly misunderstood your observation this will allow processing to continue until you either find a match or fall out of the loop completely.

    Further, because you just didn't ask a clear enough question, it's just about impossible to determine what else is going on here, but I'm going to go out on a limb here and say that immediately after the loop (i.e. if you get there a match wasn't found), you're going to simply want to do this:

    return false;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭