简体   繁体   中英

How do I get the result from a MySQL DATEDIFF query as a variable to pass to a PHP function?

I am trying to get the difference in seconds between the date/time in the column 'last_visited' in my table 'users' and the current date/time for a particular user. I think the following query should do it:

$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) from users WHERE username='$user'";
$result=mysql_query($query) or die(mysql_error());

However, I am not sure how to get the result from this as a variable so that I can pass it to a PHP function. Could someone help with this?

$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) AS time_difference from users WHERE username='$user'";
                                                                   ^

I guess it will be easier to use this variable if it has a proper name.

Return the result with a name:

$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) AS time_diff from users WHERE username='$user'";
$result=mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_assoc($result)) 
{ 
 $diff= $row['time_diff']
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM