在php代码中不显示空字段或空字段或空字段

时间:2017-12-28 12:16:57

标签: php mysql

下面是我的代码,在employee表中有一些记录,其pfuidno为null 但它没有在服务器上运行显示我的错误

$sql1="select a.code,a.empname,a.pfuidno from emplmast a ";1
$get1=mysqli_query($conn,$sql1) or die(mysqli_error());
$m_no = mysqli_num_rows($get1);
if ($m_no!=0)
{
    while($row1 = mysqli_fetch_array($get1))
    {
        if ($row1['pfuidno']=='' OR empty($row1['pfuidno']))
        {
          $m_errmsg='UAN is Blank for Employee Code '.$row1['code'].' Name '.$row1['empname'];
          echo "<script language='javascript' type='text/javascript'>alert('$m_errmsg')</script>";
        }
    }
}

1 个答案:

答案 0 :(得分:2)

1.在查询代码行旁边删除1

2.在查询中指定列名称时使用_assoc()。( 不强制 ,但会为您提供更轻的关联数组)

3. OR需要||

4.需要添加NULL的检查

代码必须是: -

$sql1="select a.code,a.empname,a.pfuidno from emplmast a "; // remove 1
$get1=mysqli_query($conn,$sql1) or die(mysqli_error());
$m_no = mysqli_num_rows($get1);
if ($m_no > 0){
    while($row1 = mysqli_fetch_assoc($get1)){ // use _assoc
        if ($row1['pfuidno']=='' || empty($row1['pfuidno']) || $row1['pfuidno'] === NULL){ //check for NULL
            $m_errmsg='UAN is Blank for Employee Code '.$row1['code'].' Name '.$row1['empname'];
            echo "<script language='javascript' type='text/javascript'>alert('$m_errmsg')</script>";
        }
    }
}