Why does this return Resource id #2?

Why does this return Resource id #2?

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

Stefan Bogdanescu

Founder & Senior Architect

Why Does This Return Resource id #2?

When you run echo mysql_query("SELECT COUNT(*) FROM persons"), you are not printing a count or an error message. You are printing a resource handle. In older PHP versions using the mysql_* extension, database functions return one of two things: a boolean indicating success or failure, or a "resource" representing the result set. When you treat this resource as a string and echo it directly, PHP converts the internal pointer to a unique identifier, resulting in output like Resource id #2.

The root cause is a fundamental misunderstanding of how database queries return data versus how they confirm execution. Your code executes the SQL statement successfully, but the variable $rt holds the result resource, not the actual row count. To get the number, you must explicitly extract that value from the resource before printing it.

Extracting Values from Resources

The mysql_query() function returns a resource when the query is successful. This resource is an object-like structure containing the data returned by the database. You cannot print this object directly to see its contents; you must access specific properties.

For aggregate functions like COUNT(*), the data resides in the first column of the first row of the result set. To retrieve it, you use mysql_result(). Here is how you should modify your code:

$rt = mysql_query("SELECT COUNT(*) FROM persons");

if ($rt) {
    $count = mysql_result($rt, 0, 0); // Get first row, first column
    echo "<h1>Number:</h1>" . $count;
} else {
    echo "Error: " . mysql_error();
}

The mysql_result() function takes three arguments: the resource handle, the row index (starting at 0), and the column index (also starting at 0). Since COUNT(*) returns a single scalar value, it effectively occupies the first cell of the result set. By calling this function, you convert the opaque resource into an integer or string that can be displayed correctly.

Why Resource IDs Appear

The "Resource id #2" output is PHP's way of displaying an object when it is cast to a string without a custom __toString() method defined for that specific legacy extension. Each time a new query result is created, PHP assigns it a unique internal ID. If you were to run multiple queries in the same script and echo the handles directly, you would see incrementing IDs like Resource id #3, Resource id #4, and so on.

This behavior highlights why relying on direct output of database resources is dangerous. It obscures the actual data until you know exactly how to inspect the resource properties. In modern development, this pattern is considered obsolete because the underlying library has been deprecated since PHP 5.5 due to security vulnerabilities and lack of support for prepared statements.

The Modern Approach with PDO or MySQLi

If you are maintaining legacy code using mysql_* functions, applying mysql_result() is the fix. However, if you are starting a new project, you should migrate to either MySQLi or PDO. These modern extensions return true/false on success and provide dedicated methods for fetching data that are safer and more predictable.

With MySQLi, you might use fetch_assoc():

$rt = mysqli_query($connection, "SELECT COUNT(*) as count FROM persons");
$row = mysqli_fetch_assoc($rt);
$count = $row['count'];
echo "<h1>Number:</h1>" . $count;

With PDO, the approach is even cleaner using fetchColumn():

$statement = $pdo->query("SELECT COUNT(*) FROM persons");
$count = $statement->fetchColumn();
echo "<h1>Number:</h1>" . $count;

These methods eliminate the need for manual column indexing and make the code intent clearer. They also integrate seamlessly with Laravel's query builder, allowing you to handle counts without writing raw SQL if desired. If you are building a Laravel application, leveraging Eloquent or the Query Builder ensures your count logic remains robust even as database schemas evolve.

Common Pitfalls in Count Queries

A frequent mistake when working with COUNT(*) is forgetting that it returns an integer but might be affected by string conversion issues depending on the database configuration. Additionally, if the table name in your query does not match exactly (including case sensitivity in certain OS configurations), the resource returned will be false, and attempting to fetch a column from a failed query will trigger an error or return null.

Always validate that $rt is truthy before attempting to extract values. Checking for errors explicitly using mysqli_error() or PDO::errorInfo() provides much better debugging information than relying on generic resource IDs.

By understanding the distinction between executing a query and retrieving its result, you avoid confusing runtime identifiers with actual data. The transition from legacy resources to modern fetching methods ensures your application remains secure and maintainable in the long term.

Our network