dsafew1231 2014-01-27 00:13
浏览 35
已采纳

如何用PHP检测AUTO_INCREMENTed值? [重复]

Hello !

I'd like to know, what value have just assigned A_I method in my MySQL table. I can't look for it by specifying other values, because at least two of them could be the same. I'm really stuck.

Let's say I'm using this simple code to INSERT new VALUES:

INSERT INTO my_table (`Auto_Incremented_Value`, `My_Column`) VALUES (NULL, '$my_value');

Now I need to know what is the last value of Auto_Incremented_Value column, because I have to use this value in order to assign it to another table as a link to that object.

In real I allow some people to upload images on my server & these are associated with some products which they belong to. I have two tables to handle this.The first one (products table) uses AUTO_INCREMENT & the second one (images table) doesn't, because I store there full path to the uploaded image, small description & link to the assigned product. I need to store the Auto_Incremented_Value as the link in my images table .

P.S. I'd be pleased if I could solve it with PHP.

Thanks!

</div>
  • 写回答

3条回答 默认 最新

  • douba4933 2014-01-27 00:15
    关注

    You should use MySQLI Insert ID for this.

    $query = "INSERT INTO my_table (`Auto_Incremented_Value`, `My_Column`) VALUES (NULL, '$my_value')";
    $mysqli->query($query);
    
    printf ("New Record has id %d.
    ", $mysqli->insert_id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line