开发者

MySQL giving back unique ID on duplicate, how?

Ive got a unique key on a few fields in a table. With PHP I get a nice error like it should when I try to insert the same data. What Im trying to archieve is that MySQL gives me back the primary key of the field that generated the error.

So when I insert something duplicate, is th开发者_如何学Goere any way that MySQL can give back the 'origional' ID without selecting all the fields Ive been trying to insert?

Tnx in advance


You can use mysql_error() to get a message like Duplicate entry '1' for key 'PRIMARY' which you can then parse to figure out what the duplicate key value is.


mysql can't do that.. maybe if you will make procedure or function for this it is posible but you shoud think are you realy need this?


If you have an AUTO_INCREMENT field (id, or something else) you can use ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) to have the LAST_INSERT_ID value return the value of the id column when an insert "failed". It will however, not fail anymore, which is something to take into consideration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜