mysqli最后插入ID

编程入门 行业动态 更新时间:2024-10-26 22:29:28
本文介绍了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.

参考

  • PHP mysqli_insert_id:www.php/manual /en/mysqli.insert-id.php

更多推荐

mysqli最后插入ID

本文发布于:2023-10-13 20:45:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1489028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysqli   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!