dongpiao9078 2018-01-20 14:03
浏览 99
已采纳

PHP PDO检查return是否为null

i am struggling a bit with a PDO problem. I want to check if a returned column has any value in it.

I am doing this:

function populateAltsDropdown($list)
{
    echo "<div class='dropdown'>
    <button class='btn btn-secondary btn-sm dropdown-toggle' type='button' id='dropdownMenuButton' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>
    Alts
    </button>
    <div class='dropdown-menu' aria-labelledby='dropdownMenuButton'>";

    foreach ($list as $row)
    {
        if($row['name'] != null) {
            echo "<a class='dropdown-item disabled' href='#'>" . $row['name'] . "</a>";
        }
        else
        {
            echo "No Alts";
        }

    }
    echo "</div></div>";
}

I don't know how to make $row['name'] != null work.

This is what i have on $list:

function selectAlts($conn,$character_id)
{
    $stmt = $conn->prepare('SELECT cm.alt_id,c.name FROM charactermapping cm
                            LEFT JOIN characters c
                                      ON (cm.alt_id = c.character_id)
                             WHERE c.active = TRUE and cm.main_id = ?');
    $stmt->execute([$character_id]);

    return $stmt;
}

I could have values in that list like:

 ID--------Name
"1002303","Name 1"
"2132444","Name 2"
"4324323",null
"4343433",null

I want some help in filtering our the values that do not have any name in them, the ones with null.

  • 写回答

1条回答 默认 最新

  • doujuchuan9915 2018-01-20 14:11
    关注

    Try to use is_null or === operator. Such as

    if(!is_null($row['name']))
    

    or

    if($row['name'] !== NULL))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?