鍍金池/ 問(wèn)答/PHP  Linux  數(shù)據(jù)庫(kù)  網(wǎng)絡(luò)安全/ 在高并發(fā)環(huán)境下,用PHP向MySQL5.5寫(xiě)入記錄,如何返回剛插入的記錄的id?

在高并發(fā)環(huán)境下,用PHP向MySQL5.5寫(xiě)入記錄,如何返回剛插入的記錄的id?

在高并發(fā)環(huán)境下,用PHP向MySQL5.5寫(xiě)入記錄,如何返回剛插入的記錄的id?

高并發(fā)
高并發(fā)
高并發(fā)

如何返回當(dāng)前插入的那條記錄的id?
MySQL版本是5.5

回答
編輯回答
焚音

mysql_insert_id(), 高并發(fā)下也是可用的.

2017年3月11日 12:42
編輯回答
筱饞貓

mysqli:mysqli_insert_id()
mysql:mysql_insert_id()

2017年7月19日 07:47
編輯回答
入她眼

高并發(fā)情況就不應(yīng)該直接寫(xiě)入數(shù)據(jù)庫(kù),應(yīng)該先給數(shù)據(jù)生成一個(gè)具有唯一性的hash值,然后寫(xiě)入到緩存。同時(shí)給客戶端返回的是hash值。

再將緩存數(shù)據(jù)異步寫(xiě)入到數(shù)據(jù)庫(kù)。

通過(guò)hash值去查數(shù)據(jù)庫(kù)就能解決。

2017年2月12日 10:35
編輯回答
心癌
SELECT 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.

2017年6月11日 03:38