简体   繁体   English

IF ELSE语句基于count> 1或= 1来执行不同的函数MY SQL php

[英]IF ELSE statement based on count > 1 or =1 to execute different functions MY SQL php

I have a table in MY SQL that may either have an entry in a certain column as 1 or > than 1. Basically based on the current Value of the Entry in the column I would wish to run either of two methods: 我在MY SQL中有一个表,可能在某个列中有一个条目为1或> 1.基本上根据列中条目的当前值,我希望运行以下两种方法之一:

Part code: 零件代码:

$db->prepare("SELECT vote_count FROM tbl_voter_count WHERE voter_phone_number  = :phone ");
$sql->bindParam(":phone", $phone );
try {

    $sql->execute();

} catch(PDOException $e) {

    echo $e->getMessage();

}

$data = $sql->fetchAll(PDO::FETCH_ASSOC);
if($data){
foreach ($data as $row) {
$count = $row['vote_count'];
 if($count == 1)
 {
   //Logic 1
 }

   //Logic 2

}

Based on the above,is there a better way of aciving this with far much less code entanglement and Lines.? 基于以上所述,有没有更好的方法来解决这个问题,而且代码纠缠和线条要少得多。

try {
  $sth->execute();
  foreach($sql->fetchAll(PDO::FETCH_ASSOC) as $row) {
   if($row['vote_count'] == 1) {
     //Logic 1
   } else {
   //Logic 2
   }
  }
} catch ...

edit I'd recommend using the catch at the end of the request. 编辑我建议在请求结束时使用catch。

Your method is fine. 你的方法很好。 You can also do: 你也可以这样做:

$db->prepare("SELECT vote_count > 1 as multivote FROM tbl_voter_count WHERE voter_phone_number  = :phone ");
$sql->bindParam(":phone", $phone );
try {

    $sql->execute();

} catch(PDOException $e) {

    echo $e->getMessage();

}

while ($row = $sql->fetch(PDO::FETCH_ASSOC) {
    if ($row['multivote']) {
        //Logic 1
    } else {
        //Logic 2
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM