Home >Database >Mysql Tutorial >How Do I Display the Result of a MySQL TIMEDIFF() Query in PHP?

How Do I Display the Result of a MySQL TIMEDIFF() Query in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-12-07 19:36:12412browse

How Do I Display the Result of a MySQL TIMEDIFF() Query in PHP?

Retrieving MySQL Query Results: Echoing TIMEDIFF() Result

In PHP, when executing a MySQL query that returns a single value, the result is stored in a PHP resource handle. To access and print the actual result, you need to use a fetch function.

To echo the result of the TIMEDIFF() query:

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

Note that the mysql_query() function is deprecated and should not be used in new projects. Instead, use PDO with PDO_mysql or mysqli for more secure and modern database interactions.

The above is the detailed content of How Do I Display the Result of a MySQL TIMEDIFF() Query in PHP?. 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