Why mysql_query Returns "Resource id #2" and How to Fix It
When you execute a query in PHP using the legacy mysql_* extension, receiving Resource id #2 as your output usually indicates that mysql_query() succeeded in sending the SQL statement to the server but failed to return the result set to your script. In your specific case, where the goal is simply to fetch a single field from wp_10_options, the error stems from how you are attempting to access the data immediately after execution.
The variable $result holds a MySQL link identifier (hence "Resource id #2"), not the actual row of data. The function mysql_query() is responsible for executing the command and returning a resource representing the result set, or a boolean true if no results are returned (like in an UPDATE). You cannot access $row['option_value'] directly from $result. That syntax expects an associative array containing the fetched data. To get that data, you must pass the result resource to a fetch function like mysql_fetch_array().
In your example, mysql_fetch_array($result) is being called correctly in the first line. However, if the output is still "Resource id #2", it implies that mysql_fetch_array() failed or was not executed as expected in your environment, or perhaps you are printing $row before it has been populated. If mysql_fetch_array() returns false, accessing a key on that false value can sometimes lead to unexpected behavior depending on error reporting settings, but typically it would return false.
The most likely culprit is a silent failure due to the legacy mysql_* API being deprecated and removed in modern PHP versions (5.5+). If you are running PHP 7 or higher, the mysql_* functions do not exist at all; attempting to use them results in a fatal error, not a resource ID printout unless you are catching it strangely. However, assuming you are running an older version of PHP where these functions exist:
The query string itself looks syntactically correct for a simple select. The issue often lies in the connection state or the specific return value handling. If mysql_query returns a link-id instead of a result resource (which happens if the query is not a SELECT), you must check the error. But since your query is a SELECT, it should return a result resource.
If you are seeing "Resource id #2" when you print $row, it means $row itself is holding that resource ID string. This suggests mysql_fetch_array() might be returning a false value that is being displayed strangely, or more likely, the code snippet provided in the source material has a logical flow issue where $result is somehow assigned to $row.
Wait, looking closer at the source:
$result = mysql_query(...);
$row = mysql_fetch_array($result);
print_r($row);
If print_r($row) outputs "Resource id #2", then mysql_fetch_array() must have returned a string containing that text, which is impossible for these standard functions. The only way this happens is if you are echoing $result instead of $row, or if there is an error in how the variables are being inspected.
However, the most practical and robust solution to "Selecting one row from MySQL using mysql_* API" without a while loop is simply to ensure you are fetching the result correctly. If you know there is exactly one row, mysql_fetch_row() or mysql_fetch_assoc() works fine. The code:
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
if ($result) {
$row = mysql_fetch_assoc($result); // Prefer assoc for named columns
echo $row['option_value'];
} else {
die(mysql_error());
}
This structure explicitly checks the query result. If mysql_query fails, you get a boolean false. If it succeeds, $row gets the array.
There is a critical pitfall here: The mysql_* extension has been removed from PHP core since version 5.5.0 and completely deprecated in PHP 7.x. Relying on it today is a significant security and stability risk. The recommended approach for any new development, including handling database rows, is to migrate to PDO or the native MySQLi extensions. These modern APIs provide better error handling, prepared statements to prevent SQL injection, and are fully supported.
If you must stick with mysql_* for legacy reasons (e.g., maintaining old WordPress code), ensure that $row is actually an array before accessing keys. The pattern echo $row['option_value']; will output nothing or a notice if $row is false or not set. Always wrap the fetch in a conditional check to handle empty results gracefully, as querying for a specific option name might return no rows if the database state has changed.
Ultimately, the "Resource id #2" confusion arises from treating the result resource as the data itself. The mysql_fetch_array function bridges that gap. But the real answer is to stop using mysql_*. Migrating to PDO offers a cleaner syntax for this exact scenario:
$stmt = $pdo->query('SELECT option_value FROM wp_10_options WHERE option_name=:name', ['name' => 'homepage']);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['option_value'] ?? null;
This approach is safer, clearer, and aligns with current industry standards. If you are maintaining a legacy project, understand that mysql_fetch_array returns an array where keys can be column names or numeric indices, but it returns false on failure. Ensure your output logic accounts for this boolean return type to avoid printing unexpected resource IDs.
