Jump to content

[SOLVED] max problams mysql.


redarrow

Recommended Posts

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>";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/43903-solved-max-problams-mysql/
Share on other sites

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>";

}
?>

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";
?>

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>";
}

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>";
}
}

?>

 

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";

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";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.