redarrow Posted March 22, 2007 Share Posted March 22, 2007 Please help i am trying to select the largest date_added and echo a edit link cheers error select * from blog_comments where id='00001' and date_a dded=max'1174139794' You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''1174139794'' at line 2 <?php $select2=" select * from blog_comments where i d='".$_SESSION['id']."' and date_added=max'".$date_added."' "; echo $select2; $res2=mysql_query($select2)or die(mysql_error()); while($dat2=mysql_fetch_assoc($res2)){ echo"<table width='300' border='0' bordercolor='black' align='center'> <td align='center'> <a href='edit_comment.php?id=".$dat2['id']."&date_added=" .$dat2['date_added']." '>Edit comment</a> </td></table>"; } ?> Quote Link to comment Share on other sites More sharing options...
fert Posted March 22, 2007 Share Posted March 22, 2007 I'm pretty sure it should be $select2=" select * from blog_comments where id='".$_SESSION['id']."' and date_added=max('".$date_added."') "; Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 22, 2007 Author Share Posted March 22, 2007 select * from blog_comments where id='00001' and date_added=max('1174139794') Invalid use of group function code <?php $select2=" select * from blog_comments where id='".$_SESSION['id']."' and date_added=max('".$date_added."') "; echo $select2; $res2=mysql_query($select2)or die(mysql_error()); while($dat2=mysql_fetch_assoc($res2)){ echo"<table width='300' border='0' bordercolor='black' align='center'> <td align='center'> <a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a> </td></table>"; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2007 Share Posted March 23, 2007 SQL MAX() function takes a columnname as its argument and returns the highest value in that column. If you want the latest blog entry for user with id='x' you can <?php $select2 = "SELECT * FROM blog_comments WHERE id='".$_SESSION['id']."' ORDER BY date_added DESC LIMIT 1"; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 23, 2007 Author Share Posted March 23, 2007 ok as what i read on max() the following code should show a edit link on correct max date_added blog for editing but dosent echo any idears please cheers. query result select id, max('1174139794') from blog_comments where id='00001' and date_added='1174139794' GROUP BY '00001' $select2="select id, max('".$rec['date_added']."') from blog_comments where id='".$_SESSION['id']."' and date_added='$date_added' GROUP BY '$id' "; echo $select2; $res2=mysql_query($select2)or die(mysql_error()); while($dat2=mysql_fetch_assoc($res2)){ echo"<table width='300' border='0' bordercolor='black' align='center'> <td align='center'> <a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a> </td></table>"; } Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 23, 2007 Author Share Posted March 23, 2007 barnand thank u ur code works cheers why dosent my php future time function work The user should only see the edit button for 5 minitues cheers $select2 = "SELECT * FROM blog_comments WHERE id='".$_SESSION['id']."' ORDER BY date_added DESC LIMIT 1"; $res2=mysql_query($select2)or die(mysql_error()); while($dat2=mysql_fetch_assoc($res2)){ $future=time()+(5*60*1); if($future < $dat2['date_added']){ echo"<table width='300' border='0' bordercolor='black' align='center'> <td align='center'> <a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a> </td></table>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2007 Share Posted March 23, 2007 How is date_added defined in your table? What column type is it? Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 23, 2007 Author Share Posted March 23, 2007 id posted_by date_added title_comment blog_comment user_blog_title user_blog_date user_blog_id 00003 god 1174075217 hello hello 1174067865 00001 00003 god 1174075302 whatwhat what int 11 m8 Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 23, 2007 Author Share Posted March 23, 2007 current reading m8 future: 1174611317 date added: 1174610393 Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2007 Share Posted March 23, 2007 Then it should output for those records where the date_added col has a timestamp value that is more than 5 minutes from now. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 23, 2007 Author Share Posted March 23, 2007 i want the button link to go in 5 minutes,so the user can not edit afther 5 minutes. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 23, 2007 Author Share Posted March 23, 2007 barand i got it going here it is ok and thank you for your help cheers. $select2 = "SELECT * FROM blog_comments WHERE id='".$_SESSION['id']."' ORDER BY date_added DESC LIMIT 1"; $res2=mysql_query($select2)or die(mysql_error()); while($dat2=mysql_fetch_assoc($res2)){ $future=time()-(5*60*1); if($dat2['date_added'] > $future){ echo"<table width='300' border='0' bordercolor='black' align='center'> <td align='center'> <a href='edit_comment.php?id=".$dat2['id']."&date_added=".$dat2['date_added']." '>Edit comment</a> <br><br> <font color='yellow'>WARNING Edit Comment Last Only 5 Minutes</font> </td></table>"; } } ?> ps. this might of worked as well FROM blog_comments WHERE id='".$_SESSION['id']."' ORDER BY date_added DESC and DATE_SUB(CURDATE(),INTERVAL -5 MIN) > $date_added LIMIT 1"; Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2007 Share Posted March 23, 2007 this might of worked as well FROM blog_comments WHERE id='".$_SESSION['id']."' ORDER BY date_added DESC and DATE_SUB(CURDATE(),INTERVAL -5 MIN) > $date_added LIMIT 1"; you would need to re-arrange the query FROM blog_comments WHERE id='".$_SESSION['id']."' AND DATE_SUB(CURDATE(),INTERVAL -5 MIN) > $date_added ORDER BY date_added DESC LIMIT 1"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.