我一直在四处寻找,但大多数教程都显示了mysql_insert_id()
,但它在同一文档中。我想知道是否有一种方法可以让你获得一个专栏的最后一个id并将其回声出来。
$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;
这是我的代码,它从数据库中获取图像并将blob转换为id=1,这是图片,但作为php。这是另一个php文档的上传器,但我正在另一个页面上输入该列的最后一个id(不是图片,而是统计数字)
一点可以做到这一点的代码将非常有帮助。
这将获取具有最高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
查询完成后,立即使用:
$newId = mysql_insert_id();
它将返回最后插入的id
$query = "SELECt MAX(id) FROM images";
$result = mysql_query($query);
$row = mysql_affected_rows($result);
echo $row[0];
使用PDO如何在PDO 中返回max(id)
$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'];