php – mysqli最后插入的id

前端之家收集整理的这篇文章主要介绍了php – mysqli最后插入的id前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将图像与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: %s\n",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

原文链接:https://www.f2er.com/php/131912.html

猜你在找的PHP相关文章