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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?
  • ¥240 MapReduce应用实践 学生课程
  • ¥15 hlss视频显示AUTHORITY_INVALID
  • ¥15 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>