phpmysqlmysql-insert-id

Get last ID and display it


I've been looking around but most of the tutorials are showing the mysql_insert_id() but it's on the same document. I'm wondering if there is a way where you can get the last id of a column and echo it out.

$image = mysql_query("SELECT * FROM images WHERE id=$id");
$image = mysql_fetch_assoc($image) or die(mysql_error());

$image = $image['image'];

header("Content-type: image/jpeg");

echo $image;

This is my code that gets the image from the database and makes the blob into a id?=1 which is the picture but as php. That is a uploader from another php document but I'm typing to output the last id from that column on another page (Not the picture but the number for statistics)

A bit of code that could do that would be very helpful.


Solution

  • This would grab the row with the highest ID:

    $result = mysql_query("SELECT MAX(id) FROM images");
    $row = mysql_fetch_row($result);
    // $row[0] contains the value of the highest id