Forum Moderators: coopster

Message Too Old, No Replies

mysql_insert_id()

         

jackvull

4:52 pm on Jan 12, 2006 (gmt 0)

10+ Year Member



Hi I run an insert on my page such as:

mysql_query($sSQL) or trigger_error(mysql_error());
$_SESSION['UserLoggedIn'] = mysql_insert_id();

As this isn't run in a transaction, isn't there the possibility that mysql_insert_id() could return an incorrect number. For example, if the the same insert ran a few hundreds of a millisecond after the first one?

jatar_k

5:56 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well check this thread out

[webmasterworld.com...]

also, why use the id as the value for 'UserLoggedIn', seems strange. If the login was successful then assign something to that var like 'true'. I actually never used a loggedin var in my session. If the user managed to login then the session should have some specific structured info in it, if it doesn't then it isn't a proper session that I created.

timster

6:01 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know exactly how PHP solves this problem, but they do solve it.

mysql_insert_id() returns the id of the row inserted in that page's previous insert, even if it is not the most recent insert.

But seeing is believing:

mysql_query($insertStatement);
sleep(30); # Ample time for another insert to occur?

$rs = mysql_query($selectMaxID); # populate variable with your select statement
$array = mysql_fetch_array($rs);
echo "max id/insert id = " . $array[0] . "/" . mysql_insert_id() . "<br />";