mysqli最后插入ID [英] mysqli last insert id
本文介绍了mysqli最后插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将图像与名字,姓氏相关联...如何检索最后一行并将其插入到另一张表中?我试过$image = $mysqli->insert_id;
然后绑定,但是不起作用.有人可以帮我吗?
I would like to associate the image with firstname, lastname...how can I retrieve the last rowand use it to insert to the other table? I tried $image = $mysqli->insert_id;
then binding but it doesn't work. Can someone help me out?
$image = $mysqli->insert_id;//this should come from table2
$stmt = $mysqli->prepare("
insert into table1 (username, firstname, lastname, image)
select ?,?,?,image from table2 t2 where username = ? and t2.id = ?
");
$stmt->bind_param('sssss', $username, $fname, $lname, $username, $image);
$stmt->execute();
推荐答案
mysqli::$insert_id
-mysqli_insert_id
—返回上一次查询中使用的自动生成的ID,例如:
mysqli::$insert_id
-- mysqli_insert_id
— Returns the auto generated id used in the last query, Example:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
/* drop table */
$mysqli->query("DROP TABLE myCity");
/* close connection */
$mysqli->close();
输出
新记录的ID为1.
New Record has id 1.
参考
这篇关于mysqli最后插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文