If statement always false?

$mysqli->query() does not return the result directly, it returns a mysqli_result object.

In order to get the information you want you’d do:

$row = $result->fetch_row();

if (null !== $row) {
    echo 'Wow you have permission to enter!';
}

You need to check against NULL because fetch_row return NULL when no rows were found. If a row is found the IP in that row must be the same as the IP in the query, because you filtered on that in the WHERE clause of the query.

Also, that query should be prepared to prevent SQL Injection:

$statement = $mysqli->prepare('SELECT ip FROM `unbanned` WHERE ip=?');
$statement->bind_param("s", $ip);
$stmt->bind_result($selectedIp);
$stmt->fetch();

if (null !== $selectedIp) {
    echo 'Wow you have permission to enter!';
}

Also, in my opinion, PDO is a lot easier to work with than MySQLi for database interactions.

2 Likes