0

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;
} 
ax752
  • 142
  • 2
  • 12
  • Which part of the function is returning false? Is it when it calls `$this->validateKey()` or after the mysql loop? – Barmar Aug 21 '14 at 02:52
  • 5
    Why don't you use `SELECT COUNT(*) as found FROM api_id WHERE Banned = 1` to see if there are any of them? Then just see if `$row['found']` is more than 0. – Barmar Aug 21 '14 at 02:54
  • Post the `$this->validateKey()` code – André Muniz Aug 21 '14 at 02:54
  • 2
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Aug 21 '14 at 02:56

2 Answers2

1

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.

argamentor
  • 148
  • 5
0

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;
} 
argamentor
  • 148
  • 5