dongzhao1930 2012-04-11 22:25
浏览 117
已采纳

mysql_insert_id()可以从另一个脚本的查询中获取值

The manual for mysql_insert_id() says

Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

This statement does not indicate any scope. Is it a serverwide value? Could a server running many scripts or many instances of one script return a value for mysql_insert_id() which was not generated by the last insert performed by the script calling it?

  • 写回答

3条回答 默认 最新

  • doujiayao8433 2012-04-11 22:44
    关注

    The scope of mysql_insert_id() is the MySQL connexion. Not the user + password, but the actual connection for the current script. (Note that the MySQL connection can also be a parameter of mysql_insert_id)

    If you close and re-open the MySQL connexion, mysql_insert_id() will not return the id of the id inserted in the previous one.

    If an id has been inserted just after your adding, but by another script execution (I mean with another connexion) the mysql_insert_id() will return your ID, not the ID actually created after your but in another connection.

    Example :

    $c1 = mysql_connect($srv, $usr, $pwd);
    $c2 = mysql_connect($srv, $usr, $pwd);
    
    $sql = "INSERT INTO table1 (col1, col2) VALUES('x', 'y') "
    mysql_query($sql, $c1); // first insert
    mysql_query($sql, $c2); // second insert
    
    $id1 = mysql_insert_id($c1);
    $id2 = mysql_insert_id($c2);
    

    $id1 will be the id inserted first, $id2 the id inserted after.

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助