Trying to get property of non-object MySQLi result

Trying to get property of non-object MySQLi result

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Trying to get property of non-object MySQLi result

You are encountering a classic PHP runtime error: Notice: Trying to get property of non-object. At first glance, this looks contradictory because your var_dump clearly shows $r is a valid mysqli_result object containing the expected data. The discrepancy lies not in the database query itself, but in how the scope of the variable changes between the definition and the usage within your method.

The root cause is almost certainly that you are using the global keyword incorrectly inside a class method. In your code snippet, you declared $mysqli as a global variable at the top of the script or function scope. However, when you define var_dump($r) and subsequently check if ($r->num_rows), PHP is evaluating the object's properties in the current context.

If the global $mysqli; statement exists inside a class method, it does not automatically inject $mysqli into the local namespace as a property or a valid variable for subsequent operations if the logic flow shifts unexpectedly. More critically, the error message Trying to get property of non-object on the line if ($r->num_rows) implies that $r itself is null or undefined at that exact moment, despite being populated earlier.

Consider the lifecycle of the variable. You execute $q = 'SELECT ...', then $r = $mysqli->query($q). The var_dump confirms $r exists and has one row. However, if you have any logic between these lines that redefines $r or if the global declaration interferes with the object's visibility in a specific version of PHP, the variable might be shadowed or reset to null before the conditional check runs.

A frequent scenario causing this specific error in class methods is relying on a global database connection without passing it as an argument. While using globals was common in older PHP scripts, modern best practices and strict typing often lead to confusion when mixing global keywords with object-oriented properties. If $mysqli is actually null at the moment of execution—perhaps due to a previous failed initialization that wasn't caught—the query() method returns null. Consequently, assigning null to $r results in an empty object reference.

When you later try to access $r->num_rows, PHP attempts to read the property on a null value (or a variable that was somehow cleared), triggering the "non-object" notice. The fact that your var_dump worked earlier suggests you might have run two different iterations of the code, or the error occurs in a specific branch of logic not shown in the snippet, where $r is indeed null.

To resolve this, avoid using global inside class methods unless absolutely necessary. Instead, pass the database connection instance to your method as an argument. This ensures that the object reference remains consistent and avoids scope pollution. Additionally, always check if the query result is false or null before attempting to access properties like num_rows.

public function userExists($mysqli, $database, $username) {
    // Use prepared statements to prevent SQL injection
    $stmt = $mysqli->prepare("SELECT id FROM {$database} WHERE username = ?");
    $stmt->bind_param('s', $username);
    $stmt->execute();
    
    $result = $stmt->get_result();

    if ($result === false) {
        // Handle query error gracefully
        return null; 
    }

    // Check if rows exist using the result object safely
    if ($result->num_rows > 0) {
        return false; // Username found
    }

    return true; // Username not found
}

This approach eliminates the risk of scope issues associated with global variables and ensures that the $mysqli connection is explicitly passed where it is needed. By validating the result object immediately after execution, you prevent the runtime error from occurring when trying to access properties on a non-existent variable.

Our network