加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – mysqli最后插入的id

发布时间:2020-12-13 16:39:21 所属栏目:PHP教程 来源:网络整理
导读:我想将图像与firstname,lastname相关联…如何检索最后一行并使用它来插入到其他表?我试过$image = $ mysqli- insert_id;然后绑定,但它不工作.有人可以帮我吗? $image = $mysqli-insert_id;//this should come from table2 $stmt = $mysqli-prepare(" inser
我想将图像与firstname,lastname相关联…如何检索最后一行并使用它来插入到其他表?我试过$image = $ mysqli-> insert_id;然后绑定,但它不工作.有人可以帮我吗?
$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,$image);
 $stmt->execute();
mysqli :: $insert_id – mysqli_insert_id – 返回上次查询中使用的自动生成的ID,示例:
$mysqli = new mysqli("localhost","my_user","my_password","world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %sn",mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL,'Stuttgart','DEU',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();

产量

New Record has id 1.

参考

> PHP mysqli_insert_id: http://www.php.net/manual/en/mysqli.insert-id.php

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读