检查连续的任何字段是否为空

时间:2014-07-04 17:12:26

标签: php

我正在开发一个应用程序,如果一行中的任何字段为空,我需要单击该应用程序。如果任何字段为空,则变量$flag应为1,其他为零。 我写到这里,但我不知道接下来要做什么来检查每个字段是否为空/空。

public function profileComplete($email)
{
    global $conn;
    $flag=0;
    try
    {
        $s = $conn->prepare("SELECT * from users where emailid = : email");
        $s->bindParam(":email",$email);
        $s->execute();
        $s->setFetchMode(PDO::FETCH_OBJ);
        while($row = $s->fetch())
        {

        }
    }
}

我应该如何根据要求进行编码? 所有建议都表示赞赏。

4 个答案:

答案 0 :(得分:1)

   public function profileComplete($email)
  {
   global $conn;
   $flag=0;
  try
   {
    $s = $conn->prepare("SELECT * from users where emailid = : email");
    $s->bindParam(":email",$email);
    $s->execute();
    $s->setFetchMode(PDO::FETCH_OBJ);
    while($row = $s->fetch())
    {
          foreach($row as $r){
            if(is_null($r) || $r==''){
              $flag=1;
            }
          }
    }
  }
 }

答案 1 :(得分:1)

while($row = $s->fetch())
{
    foreach($row as $val) 
    {
        if(empty($val)) { $flag = 1; }
    }
}

'空()'假设您要捕获null,false,空字符串和零。如果您不想抓住零/假,请使用if(is_null($val) || $val == '') { $flag = 1; }

答案 2 :(得分:1)

试试这个,虽然我没有检查输出。希望它能帮到你! :)

while($row = $s->fetch()){
     foreach($row as $rowval){
          if($rowval == null || empty($rowval)){
             $flag=1;
          }
     }
}

答案 3 :(得分:1)

我使用数组过滤器并计算行数,以避免内循环。

$num_fields = 8; //should know this before hand but you could compare $row and $_row below but it would be more expensive to run.
while($row = $s->fetch()){
    $_row = array_filter($row); //removes all empty elements
    if(count($_row) != $num_fields){
       //some fields are empty
    } 
}

如果你认为0为空,这将没问题。 - 注意 - empty()也会将0视为空,如果你需要0为非空,你可以使用array_filter中的自定义函数进行回调,使用=== 0严格相等或!== 0来检查

empty()将这些值视为空 - 并将对它们返回TRUE。

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)