note 39033 deleted from function.mysql-insert-id by nlopess
| From: | nlopess@php.net | Date: | Thu, 15 Jan 2004 14:38:06 +0000 |
| Subject: | note 39033 deleted from function.mysql-insert-id by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63618@lists.php.net to get a copy of this message | ||
Note Submitter: steven.stanley@jcu.edu.au
----
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:.