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条)

报告相同问题?

悬赏问题

  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!