duanjiao6730 2013-03-14 19:00
浏览 50
已采纳

MYSQL - 查找刚添加的记录的ID

OK, so here is my scenario - you may disagree with what I'm attempting to do but I have my reasons. The user is able to upload various information to the database, I also want them to be able to upload a picture at the same time. Now for various reasons I want to store the image with the file name the ID number of the record I'm adding (I have an ID column as the primary which auto-increments). Obviously I would have to store the file extension in the database too. Now is there a way I can add the record and then know what ID was set so that I can save the image? I don't want to query and select the highest ID as that could go wrong if two people were to submit the form at the same time. Any ideas?

  • 写回答

2条回答 默认 最新

  • doupiao9318 2013-03-14 19:06
    关注

    After running an insert in PHP, you can get the AUTO_INCREMENT ID from $mysqli->insert_id (where $mysqli is a MySQLi connection object), $pdo->lastInsertId (where $pdo is a PDO connection object), or mysql_insert_id() (if you're still using MySQL, which you shouldn't be) without having to run another query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭