Jump to content

PHP MySQL Retrieve Get last record for each group


abdur_afzal

Recommended Posts

Hi,

 

I want to retrieve last record of each group. I am using the following qury and it is giving me the last record of the table not each group.

 

<?php

$tbl_name="forum_answer"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name ORDER by a_datetime DESC LIMIT 1";

// OREDER BY id DESC is order result by descending

$result=mysql_query($sql);

?>

<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1"

 

bgcolor="#CCCCCC">

<tr>

<td width="6%" align="center" bgcolor="#E6E6E6"><strong>Answer #</strong></td>

<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Answer</strong></td>

<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Name</strong></td>

<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Q ID</strong></td>

<td width="13%" align="center" bgcolor="#E6E6E6"><strong>ANS Date/Time</strong></td>

</tr>

 

<?php

while($rows=mysql_fetch_array($result)){ // Start looping table row

?>

<tr>

<td bgcolor="#FFFFFF"><? echo $rows['a_id']; ?></td>

<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['a_id']; ?>"><? echo $rows

 

['a_answer']; ?></a><BR></td>

<td align="center" bgcolor="#FFFFFF"><? echo $rows['a_name']; ?></td>

<td align="center" bgcolor="#FFFFFF"><? echo $rows['question_id']; ?></td>

<td align="center" bgcolor="#FFFFFF"><? echo $rows['a_datetime']; ?></td>

</tr>

 

<?php

// Exit looping and close connection

}

mysql_close();

?>

<tr>

<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create

 

New Topic</strong> </a></td>

</tr>

</table>

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.