ZeroSuf3r
ZeroSuf3r

Reputation: 2001

php mysql info retrieve

if ($cuser->loggedin()){
    if (!empty($_POST['returnto']))
    {
        header("Location: ".htmlspecialchars($_POST['returnto']));
    }
    else
    {
        $query = ("SELECT id, username, check FROM users WHERE id=".$CURUSER['id']);
        $result = do_mysql_query($query);
        while($row = mysql_fetch_assoc($result))
{
    if ($row['check'] == true) {
        echo 'omg';
    } else {
echo 'omg false';


    }
    //To stop script executing next code ant print info...
    die();
}

    }

    die();
}

But always get: omg even if i chnage it to false. Any ideas ?

Upvotes: 2

Views: 164

Answers (2)

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

This is always TRUE in your case

if ($row['check'] == true) {...}

same as

if ($row['check']) {...}

Check for a $row['check'] value instead...

if ($row['check'] == 'true') {...} 

...if you have strings true and/or false as values of check column.

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237885

When you get results with mysql_fetch_assoc, the types of the values in the array will almost always be strings (they may occasionally be null, but that's not the problem here).

Any non-empty string will evaluate to true if you compare them with the == comparison operator.

The actual value being returned will not be true. It may be a string containing the word true or the word false. However, both of these will evaluate to true when compared to the boolean true.

Exactly what you should compare against depends upon what's in your database...


Your comment suggests that the field is an enum. This gives you two possible strings, "true" and "false". Since in PHP "false" == true is correct behaviour (because non-empty strings evaluate to true), you need to compare against a string value instead:

if ($row['check'] == "true") {

Upvotes: 2

Related Questions