"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to access MySQL response value in PHP

How to access MySQL response value in PHP

Posted on 2025-04-30
Browse:140

How Do I Access MySQL Response Values in PHP?

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:

  • mysql_fetch_assoc() retrieves the first row from the result set and converts it into an associative array.
  • echo $data['time_delta'] prints the value of the "time_delta" column from the first row.

Caution:

The mysql functions are deprecated and it is recommended to use the PDO or mysqli extensions instead for database handling.

Latest tutorial More>

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