Problem in fetching rows from mysql in php? |
| |
|
|
|
New Poster Posts: 2 Topics: 2
|
|
|
February 06, 2010, 04:03:19 AM
|
|
HI Friends, I tried: I inserted a row in my sql using php in the first web page and second web page i tried to retrieve it. I got: But in the next page i did not get the latest inserted row. When inserted the another row, i was able to get the first inserted row and not the latest row.
So every time i miss the last inserted row the first time and when then i was able to get it as soon as i insert another row.
How to solve this issue? Is it something related to commit i am missing here?
*** edited to remove personal promotion. we only allow promotions through signatures ***
« Last Edit: February 07, 2010, 12:29:11 AM by Admin »
|
|
| |
|
|
|
Code Guru
Location: India
Gender:  Posts: 1221 Topics: 95
|
|
|
February 07, 2010, 12:26:49 AM
|
|
If you are using some ID field/column in the table then you can make it as primary key and can set auto-increment property on that ID column. The auto-increment property on the column will make that column to have highest ID number. But for this you will need to have Data Type of the ID column as integer. So now, every time you insert a new row into the table, you can fetch the latest row by this ID column using a query:
Select MAX(ID) from table_name
Here the MAX(ID) will be the highest ID number in the table and therefore the latest row in the table.
There is one another way to achieve this, if you do not want to use ID column. You can use some Date filed/column in the table which will store the date and time at which the row is inserted in the table. So in this case, the row with the latest date will be the latest row in the table.
|
|
| |
|
|
|
New Poster Posts: 4 Topics: 0
|
|
|
June 27, 2010, 10:59:11 AM
|
|
First of all your issue is not clear. . . . Adding to the above reply. . . . . just try this one also I think you are having an issue retrieving the latest update from DB if so, here is the solution to Query the last row which was inserted in a database table $sql = "SELECT this FROM table WHERE this=this ORDER BY userid DESC LIMIT 1";
nAuGty pRoGrAmMeR
|
|
| |
|
|
|
New Poster
Location: Worcester, United Kingdom
Gender:  Posts: 1 Topics: 0
|
|
|
August 12, 2010, 02:45:11 AM
|
|
Hi Jacky Rock, This is purely down to sorting. When you call data back from a database, you'll get X amount of rows depending on how exactly you've called the data from the database. If you're getting only one row back then chances are the order is just wrong.
You need to append something like this to the end of your query "ORDER BY `fieldname` DESC" or "ORDER BY `fieldname` ASC" depending on the content of the field (one sorts one way, the other sorts the other way.
In the end you'll have a query that looks something like this - "SELECT * FROM `tablename` ORDER BY `fieldname` DESC LIMIT 1" - keep in mind that you generally need an auto-incrementing row in the database for this to work (called for example 'recordid' so the first entry will be 1, second will be 2, etc) so you know what order they have been inserted in and can sort them easily.
Thelma Customer Relations Executive Namesco - PoundHost
Namesco Shared Hosting PoundHost Dedicated Servers
|
|
| |