您好,我需要在交易的评论字段中提供交易 ID
mysql_query("INSERT INTO `transactiontb` (`tid`, `amount`, `comment`) VALUES (NULL,'$amount', CONCAT('Transaction # ',`tid`)')");
我该怎么做?
最佳答案
- 摆脱 mysql_* 函数。查看 MySQLi 或 PDO。
- 不要存储
tid
两次。为什么在选择它时不连接而不是这样存储呢?这不是最好的方法。
作为引用,请尝试LAST_INSERT_ID()+1)
:
INSERT INTO `transactiontb` (`tid`, `amount`, `comment`)
VALUES (NULL, '$amount', CONCAT_WS('Transaction # ', LAST_INSERT_ID()+1))
LAST_INSERT_ID()
将给出前一个 INSERT 的 ID,而不是当前 INSERT 的 ID,因此您必须添加 1。
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
关于php - Mysql,如何读取同一插入行的其他字段中的自增值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27925926/