Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Posts
    150

    Unanswered: Displaying only last record

    Hi,

    How can i display only the last record that was added in the db with a php function?

    Otherwise, how would this be done?

    thanks!
    Cheers!

    Mr.Crud

    There are 10 kinds of people, ones that understand digital, others that dont

  2. #2
    Join Date
    Jan 2004
    Posts
    35

    Post one way -- not the best way

    Here is one way. I wouldn't say it is the cleanest way, but it will work in a bind. One thing that you is a minor stipulation is in your table you would have to have some sort of auto_increment field. So everytime you insert a new row that new field has a new higher number. The last one inserted would have the highest number. In this example 'id' is that auto_increment field.


    $select = "SELECT *
    FROM `table`
    ORDER BY id
    desc
    LIMIT 0, 1 ";

    $run_query = mysql_query($select);
    while ($thearray = mysql_fetch_array($run_query)) {
    $id = $thearray[id];
    $field2 = $thearray[field2];
    $field3 = $thearray[field3];
    }

    echo "The Last row contains the following Information: <br>";
    echo "$id -- $field2 -- $field3";


    I havn't tested this with a PHP server, so I hope I have all my sytax right
    Last edited by pebkac; 12-08-04 at 21:03.

  3. #3
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    There is a function to find thelast inserted id. If your ID is auto incremented the you can use like this

    $result=mysql_query("select * from tablename where `id`=".mysql_insert_id());
    $row=mysql_fetch_array($result);
    $id=$row['id'];
    ...
    ..
    ..

    this would be one another method.

    In the post done by pebkac.. it will work but there is a small mistake in the code. You don't have to use the while loop there.

    Regards
    Rajesh
    Freelance and Technology Consultant
    -------------------
    Dreams are for ever
    -------------------

  4. #4
    Join Date
    Nov 2003
    Posts
    150

    Talking

    Thanks guys, i knew there was an easy way to do this. I do have an auto-increment field in my DB, so it had to be simple.

    Will let you know when i try it!

    Cheers!
    Cheers!

    Mr.Crud

    There are 10 kinds of people, ones that understand digital, others that dont

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •