這個問題以前絕壁遇到過,太久沒寫不記得(貌似當時是CI架構直接有相關函數的),然後這次又遇到了,再次滾去查了一下,這裡說的並非是PDO之類的情況,而是用過時的串連和執行方式之後怎麼進行操作。
有什麼SQL語句實現的,但明顯不合適,當收到多人操作時,頓時就混亂不堪。
所以在此,用mysql_insert_id()函數搞定,他會返回AUTO_INCRESEMENT的值。
| 代碼如下 |
複製代碼 |
<?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('mydb'); mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf("Last inserted record has id %d ", mysql_insert_id()); ?> |
剛開始我還在想這個函數不會遇到相同的問題麼,然後小新告訴我是基於當前資料庫連接的,頓時不怕不怕啦。
當然還有
| 代碼如下 |
複製代碼 |
mysql_query("select max(id) from t1",$link); |
當然,現在這種串連方式已經out的可以了,
這裡也有說明:
Warning
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_insert_id()
PDO::lastInsertId()