note 39033 added to function.mysql-insert-id

From: Date: Thu, 15 Jan 2004 04:20:15 +0000
Subject: note 39033 added to function.mysql-insert-id
Groups: php.notes 
Request: Send a blank email to php-notes+get-63579@lists.php.net to get a copy of this message
I worked around this issue before I discovered this function, (yes yes - RTFM, I know) which might be useful if this isn't doing what you want it to do for some other reason. In essence I submitted the Insert stuff with a timestamp, and then subsequently used the timestamp to extract the resultant auto-incremented ID from the new record - this may catch the unsuccessful Insert issue mentioned elswhere here, as the timestamp wouldn't exist for the new record either. code bits related <?php //get date in SQL timestamp compatible format $date = date("Y-m-d H:i:s"); //clip... //user does not exist so enter details into database $query = "INSERT INTO yourTableHere ( PersonID , firstName , lastName , xtnNum , School , roomNum , dateCreated) VALUES ('', '$firstName' , '$lastName', '$extNum', '$School', '$roomNum', '$date')"; //<-- there's the date inserted $result = mysql_query($query) or die("Problems entering your personal details blah blah (". mysql_error().") and request assistance." ); //Once new user record is created, then get resulting User ID using the timestamp to identify the record just created $query = "SELECT * FROM yourTableHere WHERE dateCreated = '".$date."' LIMIT 0, 30"; $result = mysql_query($query) or die("Failed Query! - " . $query); //do the query or report the error $myrow = mysql_fetch_row($result); $personID = $myrow[0]; // index position depends on your table structure // test print print "<p> Person ID extracted is ".$personID.".</p>"; //end clip ?> Sorry if this is a bit verbose, but it works and may be useful if you can't use the function for some reason... I don't know how well it would work on a site that was running hot - ie multiple exact same timestamps? Regards .:S:. ---- Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39033 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39033&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39033&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63579) next »