0

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.

4
  • change the sql query to exclude null or empty values? Commented Jan 20, 2018 at 14:04
  • If you con't care about the null values, use an INNER JOIN instead Commented Jan 20, 2018 at 14:04
  • I need the null values as the alt_id is used somewhere else, however not all of them have names, and i need to display the ones that have but leave out those that don't. Commented Jan 20, 2018 at 14:07
  • try $row['name'] !== null Commented Jan 20, 2018 at 14:09

1 Answer 1

3

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

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

or

if($row['name'] !== NULL))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.