If $port is a string, such as "3306", mysqli::query() will not work, even though mysqli_connect_errno() reports no error (value 0)!
$mysqli = new mysqli("localhost", "user","password","database", "3306"); //port is a string!
// Fatal error: Call to a member function fetch_assoc() on a non-object
This is particularly strange, since mysqli::query() should return a result object, or a boolean.
So, be careful when you read your port from a string or config file. Cast it to int first:
$port = (int)$port;