简体   繁体   English

如何获取最后插入的 ID?

[英]How to get the last inserted ID?

I am trying to fetch the last inserted ID from MySQL database in PHP and I was wondering what would be the best way to do it?我正在尝试从 PHP 中的 MySQL 数据库中获取最后插入的 ID,我想知道最好的方法是什么?

I am currently using this kind of solution, but there has to be a better and safer way.我目前正在使用这种解决方案,但必须有更好更安全的方法。 So would someone please help me?那么有人可以帮助我吗? Thanks谢谢

Some insert statement...

 $query = "SELECT MAX(aid) FROM asset";
 $result = mysql_query($query);
                            while($row = mysql_fetch_array($result, MYSQL_ASSOC))
                                    {
                                    $pickassssult= $row['MAX(aid)'];


                                    }

PHP has a function called: PHP 有一个 function 称为:

 mysql_insert_id();

more info http://lt.php.net/manual/en/function.mysql-insert-id.php更多信息http://lt.php.net/manual/en/function.mysql-insert-id.php

See the manual: mysql_insert_id参见手册: mysql_insert_id

MySQL command: last_insert_id() MySQL 命令: last_insert_id()

You can get the insert id by calling the mysql_insert_id() function.您可以通过调用 mysql_insert_id() function 来获取插入 ID。 See mysql_insert_id()mysql_insert_id()

Use mysql_insert_id() .使用mysql_insert_id() Hovewer, it's not absolutely safe.然而,这并不是绝对安全的。 Here is a manual这是一个手册

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

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