473,372 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 14528
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

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

Similar topics

1
by: Matt | last post by:
how to get the last record from database without looping? Whenever the user need to insert a new record to the database, it just increment the id field by one from the last record. I tried...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
3
by: Mark | last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields out of the very last record in the db. How do I do this? Actually I'm after the primary key, titled 'AdID' it'll tell...
2
by: Terry | last post by:
Any .dll or COM+ for .Net can help me retrive data from a Access file? The table contain several columns and the last one is a container which I store Article; because each article is a little bit...
0
by: babu | last post by:
Can Anybody help me about this issue, that how to retrive ole object(datatype) in MS Access database what variable i have to use to store data in VB.NET .The Format of the ole object stored like...
8
by: asenthil | last post by:
Hai, i'm having a string in a specific field of a database... now i want to retrive that string from the database and i have to write that string into a xml file.... retriving is not a...
0
nehashri
by: nehashri | last post by:
hi, I need one help regarding Asp with MsAccess database. Problem. ======== i want to retrive stored image from the MSAccess database using asp.
2
by: gahoi | last post by:
all off you hru we need to help for one problum create to me in database i am develop the login page and we save the user name, password and email address in table and we have retrive the...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.