ax752
ax752

Reputation: 142

My function is returning false when it should return true

My function is always returning false when it should return true, and I can't find why

public function isReselling($key)
{
    if ($this->validateKey($key)) {
        return false;
    }
    $apis = mysql_connect("mysql.hostinger.fr", "u770656121_uapi", "testpass") or die(mysql_error());
    mysql_select_db("u770656121_api", $apis);
    $sql = "
    SELECT * FROM api_id
    ";
    $result = mysql_query($sql, $apis);
    while($row = mysql_fetch_array($result)) {
        $blacklisttho = $row['Banned'];
        if ($blacklisttho == 1) {
            return true;
        }
    }
    return false;
} 

Upvotes: 0

Views: 112

Answers (2)

argamentor
argamentor

Reputation: 148

I would change your code to something like:

public function isReselling($key)
{
    $retValue = false;

    if ($this->validateKey($key) === false) {
        $apis = mysql_connect("mysql.hostinger.fr", "u770656121_uapi", "testpass") or die(mysql_error());
        mysql_select_db("u770656121_api", $apis);
        $sql = "SELECT * FROM api_id";
        $result = mysql_query($sql, $apis);
        while($row = mysql_fetch_array($result)) {
            if ($row['Banned'] == 1) {
                $retValue = true;
                break;
            }
        }
    }
    return $retValue;
} 

Upvotes: 0

argamentor
argamentor

Reputation: 148

Well, you need to check where exactly the 'return' is beign made, and investigate based on that:

public function isReselling($key)
{
    if ($this->validateKey($key)) {

       die('validate fails');

        return false;
    }
    $apis = mysql_connect("mysql.hostinger.fr", "u770656121_uapi", "testpass") or die(mysql_error());
    mysql_select_db("u770656121_api", $apis);
    $sql = "
    SELECT * FROM api_id
    ";
    $result = mysql_query($sql, $apis);
    while($row = mysql_fetch_array($result)) {
        $blacklisttho = $row['Banned'];
        if ($blacklisttho == 1) {
            return true;
        }
    }

    die('no results.');

    return false;
}

and btw, you don't want to have multiple 'returns' around the code, that's bad practice.

Upvotes: 1

Related Questions