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
Linda HamiltonOriginal
2024-12-03 03:08:10461browse

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'];
}

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!

Statement:
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