简体   繁体   English

获取表的最后一条记录

[英]get last record of a table

I have a form that lets users create new records, 我有一个表格,可以让用户创建新记录,
In the field that is the id that auto-increments i want the user to see the record number that this record is by somehow showing latest value+1 in the fields value. 在要自动增加的ID的字段中,我希望用户通过某种方式在字段值中显示此记录的最新记录号。

I was thinking of something like: 我在想类似的东西:

<input type="text"  value="<?php echo $myQuery['recordId'].length+1"/> 

But that doesn't work. 但这是行不通的。

Again, this is only to get the default value in the <input> 同样,这只是为了获取<input>的默认值

Instead of having to look up the last id, And this form is only used by one admin. 无需查找最后一个ID,并且此表单仅由一个管理员使用。

You can find the one plus the highest id by selecting it: 您可以通过选择找到一个加最高的ID:

SELECT MAX(id)+1 FROM table

But like David said, you're not guaranteed this will be the id that is used, because 2 people could load the page at about the same time. 但是就像David所说的那样,您不能保证这将是所使用的ID,因为2个人可能会同时加载该页面。

To get the last id relevant to that connection, use mysql_insert_id 要获取与该连接相关的最后一个ID,请使用mysql_insert_id

In your case you'll have to insert an empty record in the db before you can guarantee that it will count. 在您的情况下,您必须在db中插入一个空记录,然后才能保证它会计数。 It will leave a lot of empty records if the users don't proceed, but you can do a cleanup every time the form is loaded by deleting records created more than one hour ago that don't have a title value or something like that. 如果用户不继续操作,它将留下很多空的记录,但是您可以在每次加载表单时进行清理,方法是删除一个多小时前创建的没有标题值或类似内容的记录。

if you absolutely need it to be an integer, you can always create a special table with only one auto_increment column, insert to it, get the last_insert_id() and use that. 如果绝对需要将其作为整数,则始终可以创建仅具有一个auto_increment列的特殊表,将其插入其中,获取last_insert_id()并使用它。 this is kind of mimicking the sequences in oracle. 这有点像甲骨文中的序列。 the ids that dont get used will go waste, but the other problems associated with multiuser scenarios will not occur. 不使用的ID将会浪费,但是不会发生与多用户方案相关的其他问题。 The following code copied from mysql documentation on information functions . 以下代码是从mysql文档中复制的有关信息功能的代码。 Go there to read on the promises of this code. 去那里阅读此代码的承诺。

CREATE TABLE sequence (id INT NOT NULL);
INSERT INTO sequence VALUES (0);
UPDATE sequence SET id=LAST_INSERT_ID(id+1);
SELECT LAST_INSERT_ID();

Now back to my own words. 现在回到我自己的话。 If it does not have to be an integer you can use guid . 如果不必一定是整数,则可以使用guid There is a uniqid function in php, and there is a uuid function in mysql. 在php中有一个uniqid函数,在mysql中有一个uuid函数。 the theory says even if everyone keeps generating guids independently all the time, every guid will be unique. 该理论说,即使每个人都始终保持独立地生成向导,每个向导也将是唯一的。

So this is one way of doing it: 因此,这是一种实现方式:

<?php
    $query = "select uuid()";
    $result = mysql_query($query);
    $row = mysql_fetch_row($result);
    $uuid = $row[0];
?>
<input type="text" value="<?php echo $uuid; ?>"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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