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

}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

  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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.