[英]Return the ID of the last value inserted SQL
I'm inserting data into members table, after I've inserted it I want to get the userID of the information just inserted. 我将数据插入成员表,插入后,我想获取刚刚插入的信息的用户ID。 Whats the best way about doing this ?
最好的方法是什么?
My php code 我的PHP代码
//Insert info into database
$sql = "INSERT INTO members (type, firstname, lastname, email, password, bio) VALUES ('$usertype', '$firstname', '$lastname', '$emailsighup', '$passwordsighnup', '$bio')";
//Run a query to check if data has been inserted correctly.
$records = mysqli_query($connect, $sql);
I did try this SQL but was getting errors 我确实尝试过此SQL,但出现错误
$sql = "INSERT INTO members (type, firstname, lastname, email, password, bio) VALUES ('$usertype', '$firstname', '$lastname', '$emailsighup', '$passwordsighnup', '$bio')"; SELECT SCOPE_IDENTITY(userID);
Assuming $connect
as the variable holding your connection information, which seems legit, you can get the value of the last id with: 假设
$connect
是保存您的连接信息的变量,这似乎是合法的,则可以使用以下命令获取最后一个id的值:
$user_id = mysqli_insert_id($connect);
This is something you can do from the PHP MySQLi interface , rather than directly from SQL! 您可以从PHP MySQLi接口执行此操作 ,而不是直接从SQL!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.