Accessing MySQL Response Values in PHP
In PHP, when querying a MySQL database, the result is stored in a resource handle. This can lead to confusion when attempting to print or use the response data.
Problem:
Consider the following code:
$datos1 = mysql_query("SELECT TIMEDIFF(NOW(), '" . $row['fecha'] . "');");
echo($datos1);
This code returns "Resource id #6" instead of the expected value.
Solution:
To access the actual response data, you need to use a fetch function. Here's an updated example:
$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:
The mysql functions are deprecated and it is recommended to use the PDO or mysqli extensions instead for database handling.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3