dongmimeng5500 2014-07-04 17:12
浏览 15
已采纳

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

I'm developing an application in which I need to click if any of the fields in a row is empty or not. If any of the fields is empty, then the variable $flag should be 1, other wise zero. I have written till here, but I'm not sure what to do next to check if each and every field is null/empty or not.

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())
        {

        }
    }
}

How should I code to make it work according to the requirements? All suggestions are appreciated.

  • 写回答

4条回答 默认 最新

  • dongzj2015 2014-07-04 17:30
    关注

    Id go with array filter and count the row, to avoid the inner loop.

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

    if you consider 0 empty this will be fine. - note - that empty() will also treat 0 as empty, if you need the 0 as not empty you can use a custom function in array_filter for the callback with the === 0 strict equality or !== 0 to check for that.

    empty() treats these values as empty - and will return TRUE on them.

    "" (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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算