简体   繁体   English

获取刚插入MySQL数据库的行ID

[英]Getting id of row just inserted into MySQL database

I have my table columns set like this: 我将表列设置如下:

likes(id, like_message, timestamp)

id is the primary key that is auto incrementing. id是自动递增的主键。 This is the SQL that I use to add a row: 这是我用来添加行的SQL:

$sql = "INSERT INTO `likes` (like_message, timestamp)
        VALUES ('$likeMsg', $timeStamp)";

Everything works, but now I need to throw back the id attribute of the newly inserted row. 一切正常,但是现在我需要放弃新插入的行的id属性。 For example, if I insert a row and the id of that row is 13 , I need to echo out 13 so my AJAX request can pick that up and use it. 例如,如果我插入一行并且该行的id13 ,则需要回显13以便我的AJAX请求可以选择并使用它。

Any help would be appreciated, as well as related code samples. 任何帮助以及相关的代码示例,将不胜感激。 Thanks :) 谢谢 :)

This page will give you lots of good information on the problem: 该页面将为您提供有关该问题的很多有用信息:

MySQL: Getting unique ID MySQL:获取唯一ID

In PHP: 在PHP中:

mysql_query("INSERT INTO foo(blah) values ('blee')");
echo mysql_insert_id();

In SQL: 在SQL中:

INSERT INTO foo (blah) VALUES('blee');

LAST_INSERT_ID()   --now contains your last inserted id

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

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