简体   繁体   中英

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.

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
SELECT * FROM images ORDER BY id DESC LIMIT 1

Immediately after the query is done, use:

$newId = mysql_insert_id();

It will return the last inserted id

With PDO How to return max(id) in pdo

$sql_lastid = $pdo -> prepare("SELECT MAX(id) as max_id FROM *table*");
$sql_lastid -> execute();
$id     = $sql_lastid -> fetch(PDO::FETCH_ASSOC);
$lastid = $id['max_id'];
$query = "SELECt MAX(id) FROM images";

$result = mysql_query($query);
$row = mysql_affected_rows($result);

echo $row[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM