469,360 Members | 1,600 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

How to retrive only the last inserted row from the database

Hi all. I am trying to get the input from a form to print it out on a page where the information filled on a form must be on the page in a letter manner.But now what is happening I do that but it displays everything in the database , while I only want the last entry to be displayed.To the letter.I dont have id on my table so cant use last_insert_id()

Any I dea to how I do that .Thanks in advance
Nov 22 '06 #1
10 14132
ronverdonk
4,258 Expert 4TB
There are a 100 ways to do this, but without a display of your code, it's no use. So show the code you are using now.

Ronald :cool:
Nov 22 '06 #2
There are a 100 ways to do this, but without a display of your code, it's no use. So show the code you are using now.

Ronald :cool:
Here is my code....... It is supposed to come out in this letter and only the last entrys information needs to be filled in.The select part surely is wrong also coz I select * from the table not sure how to pu it .Thanx

<?
$username="root";
$password="issasql";
$database="assets";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM product";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

print '<b>I '.$issued_to.' hereby acknowledge full responsibilty for the equipment
listed below. I acknowledge and fully understand that I will be held
liable for any loss or damage (Software, Hardware or both) cuased to the
equipment whilst in my possession.


In the case of theft, loss or damage, I understand that I will be responsible for
replacing the equipment lost, stolen or damage at their going
market price.

This equipment should be returned in the original condition as recieved.</b><br><br>

<tr> <tr>
<td> <td>
TRANSFERED FROM</b><br><br> TRANSFERED TO</b><br><br>
Responsible Person:</b><br><br> Responsible Person:</b><br><br>
Section:</b><br><br> Section:</b><br><br>
Signature:</b><br><br> Signature:</b><br><br>
Date:</b><br><br></td></tr> Date:</b><br><br></td></tr>';

$i=0;
while ($i < $num) {

$description=mysql_result($result,$i,"description" );
$serial_number=mysql_result($result,$i,"serial_num ber");
$unique_dept_num=mysql_result($result,$i,"unique_d ept_num");
$issued_to=mysql_result($result,$i,"issued_to");
$issued_from=mysql_result($result,$i,"issued_from" );
$section=mysql_result($result,$i,"section");
$issue_date=mysql_result($result,$i,"issue_date");
$return_date=mysql_result($result,$i,"return_date" );

echo "<b>Description :$description </b><br>Serial Number:$serial_number</b><br>Unique Department Number:$unique_dept_num<br>Issued To: $issued_to<br>Issued From: $issued_from<br>Employee Section: $section<br>Date of Issue: $issue_date<br>Return Date: $return_date<hr><br>";

$i++;
}

?>
Nov 23 '06 #3
ronverdonk
4,258 Expert 4TB
After 20 or so posts in this forum you know. you have been told before, that you should put your code with php, code or html tags! as stated in the Posting Guidelines.

If you want to seek help in this forum, all we ask is that you comply with a few simple rules. People who continuously refuse to do just that, and consider this forum a one-way street for their use, will not be answered by me.

Ronald :cool:
Nov 23 '06 #4
After 20 or so posts in this forum you know. you have been told before, that you should put your code with php, code or html tags! as stated in the Posting Guidelines.

If you want to seek help in this forum, all we ask is that you comply with a few simple rules. People who continuously refuse to do just that, and consider this forum a one-way street for their use, will not be answered by me.

Ronald :cool:
[php]
$username="root";
$password="issasql";
$database="assets";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM product";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

print '<img src="3dcoatl.jpg">



[html]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h2>CERTIFICAT E FOR THE TRANSFER OF ASSETS/EQUIPMENT</br>
TRANSFERED FROM ONE PERSON TO ANOTHER </h2>




<b>I '.$issued_to.' hereby acknowledge full responsibilty for the equipment
listed below. I acknowledge and fully understand that I will be held
liable for any loss or damage (Software, Hardware or both) cuased to the
equipment whilst in my possession.


In the case of theft, loss or damage, I understand that I will be responsible for
replacing the equipment lost, stolen or damage at their going
market price.

This equipment should be returned in the original condition as recieved.</b><br><br>

<table width="100%" border="1">
<tr>
<td>Description</td>
<td>Serial Number</td>
<td>Unique Dept No</td>
<td>Quantity</td>
</tr>

<tr>
<td>$description </td>
<td>$serial_number</td>
<td>$unique_dept_num</td>
<td>$quantity</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<table width="100%" border="0">
<tr>
<td>TRANSFERED FROM</td>
<td>TRANSFERED TO</td>
</tr>
<tr>
<td>Responsible Person:</td>
<td>Responsible Person:</td>
</tr>
<tr>
<td>Section:</td>
<td>Section:</td>
</tr>
<tr>
<td>Signature:</td>
<td>Signature:</td>
</tr>
<tr>
<td>Date:</td>
<td>Date:</td>
</tr>
</table>';
[/html]

if ($num>1) {
$to=1;
}else{
$to=$num;
}



$i=0;
while ($i < $to) {

$description=mysql_result($result,$i,"description" );
$serial_number=mysql_result($result,$i,"serial_num ber");
$unique_dept_num=mysql_result($result,$i,"unique_d ept_num");
$issued_to=mysql_result($result,$i,"issued_to");
$issued_from=mysql_result($result,$i,"issued_from" );
$section=mysql_result($result,$i,"section");
$issue_date=mysql_result($result,$i,"issue_date");


//echo "<b>Description :$description </b><br>Serial Number:$serial_number</b><br>Unique Department Number:$unique_dept_num<br>Issued To: $issued_to<br>Issued From: $issued_from<br>Employee Section: $section<br>Date of Issue: $issue_date<hr><br>";
$i++;
}

[/php]

hope this looks fine
Nov 23 '06 #5
ronverdonk
4,258 Expert 4TB
This should be easy, but that depends entirely on how to distinguish the last row inserted in the table. I also have some remarks and some errors in your code, but we will get to that after you have replied.

The big question here for you is:
How can you recognise the last row inserted in the table? You said there is no id (I assume you meant no auto_increment field), but is there maybe a timestamp, or ANY field that you can use to determine the last row inserted?
If you don't have such a field, how else can you determine what the last one is?

Ronald :cool:
Nov 23 '06 #6
This should be easy, but that depends entirely on how to distinguish the last row inserted in the table. I also have some remarks and some errors in your code, but we will get to that after you have replied.

The big question here for you is:
How can you recognise the last row inserted in the table? You said there is no id (I assume you meant no auto_increment field), but is there maybe a timestamp, or ANY field that you can use to determine the last row inserted?
If you don't have such a field, how else can you determine what the last one is?

Ronald :cool:
Yes there is no auto_increment, not sure what a time stamp is maybe it could work,the reason I do not have the auoto increment is the unique_dept_num is already a PK so is there a way I could use? I will be happy to know I cant use the auto_increment always.
Nov 24 '06 #7
the easiest way is to have a field, i just call it ID, and auto increment it and then you can either run a little while loop that reads out the ID field until it finds the bigest value although thinking about it that might not work, id have to think about that. but i will asume there is an sql function that will tell you how many records there are in a table so you then simply use that value and go

[PHP]
$resource = mysql_query('SELECT * FROM `table` WHERE ID = ' . $sql_record_num . ';', $link);
[/PHP]
Nov 24 '06 #8
may this help
[php]
$row = mysql_query("select count(*) from product");
$count = $row[0];
// to get last inserted row :
$count -= 1; //last inserted row index
$lastRow = mysql_query("select * from product limit $count, 1");
[/php]
Nov 24 '06 #9
may this help
[php]
$row = mysql_query("select count(*) from product");
$count = $row[0];
// to get last inserted row :
$count -= 1; //last inserted row index
$lastRow = mysql_query("select * from product limit $count, 1");
[/php]
Thanx for the replies.

Decided to change the database structure to use id as my primary key. How do I use the id to get the last inserted row information??
Nov 24 '06 #10
ronverdonk
4,258 Expert 4TB
Since the ID is always incremented when a row is stored, the highest id will be the last. You can get this by:
[php]
SELECT * from table ORDER BY id DESC LIMIT 1;
[/php]
Ronald :cool:
Nov 24 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Mark | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.