graham23s Posted April 2, 2008 Share Posted April 2, 2008 Hi Guys, i'm trying to use the datediff in mysql to work out the days from when a member registered untill thye logged in! i have: <?php $q_date_diff = "SELECT DATEDIFF (`registered`,`last_login`) WHERE `id`='$users_id'"; $r_date_diff = mysql_query($q_date_diff); $num_days = mysql_num_rows($r_date_diff); print("Has been a member ($num_days) days."); ?> but this throws up an error i take it my syntax is off is that right? thanks guys Graham Link to comment https://forums.phpfreaks.com/topic/99222-datediff-syntax/ Share on other sites More sharing options...
mwasif Posted April 2, 2008 Share Posted April 2, 2008 mysql_num_rows() count the number of rows in the result set. <?php $q_date_diff = "SELECT DATEDIFF (`registered`,`last_login`) AS num_days WHERE `id`='$users_id'"; $r_date_diff = mysql_query($q_date_diff); $rs = mysql_fetch_array($r_date_diff); print("Has been a member ({$rs["num_days"]}) days."); ?> Link to comment https://forums.phpfreaks.com/topic/99222-datediff-syntax/#findComment-507710 Share on other sites More sharing options...
graham23s Posted April 2, 2008 Author Share Posted April 2, 2008 Hi Mate, there seems to be an error with my query i even did this in a simple state to test: <?php // date diff how many days... // $q_date_diff = "SELECT DATEDIFF (`2008-03-16 04:53:45`,`2008-04-02 11:01:08`) AS num_days WHERE `id`='$users_id'"; $r_date_diff = mysql_query($q_date_diff); $a_date_diff = mysql_fetch_array($r_date_diff); $days = $a_date_diff['num_days']; print("$q_date_diff"); print("Has been a member ($days) days."); ?> i still get: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in is it the syntaxt i have do you think? cheers Graham Link to comment https://forums.phpfreaks.com/topic/99222-datediff-syntax/#findComment-507768 Share on other sites More sharing options...
fenway Posted April 2, 2008 Share Posted April 2, 2008 Are those single quotes? Link to comment https://forums.phpfreaks.com/topic/99222-datediff-syntax/#findComment-507790 Share on other sites More sharing options...
mwasif Posted April 2, 2008 Share Posted April 2, 2008 $q_date_diff = "SELECT DATEDIFF ('2008-03-16 04:53:45','2008-04-02 11:01:08') AS num_days WHERE `id`='$users_id'"; Use single qoutes around values not the back ticks. If you still get error, use mysql_error() to know error details. Link to comment https://forums.phpfreaks.com/topic/99222-datediff-syntax/#findComment-507818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.