dongwai4434 2016-08-15 14:20 采纳率: 0%
浏览 22
已采纳

mysqli_insert_id()是从整个服务器还是从同一个用户获取记录?

I have mysql question that mysqli_insert_id() returns record from overall server or from specific user who has inserted that id? Because if it returns from overall then it won't be secure to use this function

  • 写回答

2条回答 默认 最新

  • dongzhou8764 2016-08-15 14:29
    关注

    The comment from @daan above is flat-out wrong. insert_id() returns the ID placed into an auto_increment field of the last insert query that the connection executing the insert_id() query performed.

    It does not return the largest ID in the table, it doesn't return the id created by some OTHER connection, it doesn't return the id created by some OTHER user.

    It is literally the last ID YOU created by the LAST insert YOU performed.

    That means it is 100% reliable to perform an insert, get the created ID via last_insert_id() and then use that ID in other queries to create parent/child records.

    But note that insert_id() only ever returns ONE id value. If you do a multi-value insert, you still only get the ID from the last value set, e.g.

    INSERT INTO sometable (x, y) VALUES (1,2), (2,3)
    

    still performs two inserts internally, and you'd only get the id for the 2,3 tuple, not the 1,2.

    As well, there's no memory for all previous queries:

    INSERT ...   // query #1
    INSERT ...   // query #2
    SET id = last_insert_id();
    

    will only get the ID created by query #2, and the ID from query #1 is "lost".

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条