Home >Database >Mysql Tutorial >How to Fix the 'Resource id #' Error When Echoing MySQL Query Results in PHP?
Echoing MySQL Query Results in PHP: Overcoming "Resource id #" Error
When working with MySQL databases in PHP, you may encounter the "Resource id #" error while attempting to echo the result of a query. This error message indicates that you're trying to directly echo a MySQL resource handle instead of the actual query result.
To resolve this issue and retrieve the intended result, you need to employ a fetch function. The following approach illustrates how to use the mysql_fetch_assoc() function to fetch and print the query result:
$result = mysql_query(sprintf("SELECT TIMEDIFF(NOW(), '%s') as time_delta", $row['fecha'])); if ($result) { $data = mysql_fetch_assoc($result); echo $data['time_delta']; }
In this code:
Caution:
While the mysql functions are still available, their use in new projects is strongly discouraged. Consider utilizing the PDO extension with the PDO_mysql PDO driver or the mysqli extension for improved security and maintainability.
The above is the detailed content of How to Fix the 'Resource id #' Error When Echoing MySQL Query Results in PHP?. For more information, please follow other related articles on the PHP Chinese website!