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 求解,我这个代码为什么运行不了
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错
  • ¥15 Vivado仿真数据出错
  • ¥15 银河麒麟不支持vant等前端框架怎么办?
  • ¥15 vue3的子组件在父页面调用不显示不生效问题
  • ¥15 cadence PEX
  • ¥15 phython创建了文件 显示在项目下面,但运行不了,运行时还是运行main文件,并且说main文件不允许并行运行
  • ¥15 FutureWarning:不推荐使用空或全 NA 条目的 DataFrame 串联行为。怎么改呢?