Home > Database > Mysql Tutorial > Why Does My PHP Code Show 'Resource ID #6' Instead of MySQL Query Results?

Why Does My PHP Code Show 'Resource ID #6' Instead of MySQL Query Results?

Linda Hamilton
Release: 2024-12-03 03:08:10
Original
506 people have browsed it

Why Does My PHP Code Show

Echoing a "Resource ID #6" from MySQL Results in PHP

In PHP, the mysql_query() function returns a resource ID when executing a SQL statement. This resource ID represents the result set of the query. To extract the actual result, you need to use a fetch function.

For example, to echo the result of SELECT TIMEDIFF(NOW(), '" . $row['fecha'] . "'); and avoid getting "Resource ID #6," use the following code:

$result = mysql_query(sprintf("SELECT TIMEDIFF(NOW(), '%s') as time_delta", $row['fecha']));
if ($result) {
  $data = mysql_fetch_assoc($result);
  echo $data['time_delta'];
}
Copy after login

The mysql_fetch_assoc() function retrieves the first row of the result set as an associative array. You can then access the result using the column name as the array key (e.g., $data['time_delta']).

Note:

It's generally discouraged to use the deprecated mysql_* functions. Instead, consider using PDO with PDO_mysql or mysqli.

The above is the detailed content of Why Does My PHP Code Show 'Resource ID #6' Instead of MySQL Query Results?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template