Ninjakreborn Posted March 12, 2007 Share Posted March 12, 2007 Say for example, I enter a data into a form. This form is going into a database. Upon submittal I have <?php // validate material // show errors // if clear clean variables // database variables // review form $id = mysql_insert_id(); // id = last inserted value. ?> What I am concerned about is this. 1. 2 people go to the form. Both submit at the same time Can the id on one persons script pickup the id on the record from the other guy if he submitted seconds earlier, I guess that's what I want to know. Or does it ALWAYS refer to the value entered into THAT SPECIFIC script. I just want to make sure it's reliable, before I use it. Quote Link to comment https://forums.phpfreaks.com/topic/42348-solved-php-id/ Share on other sites More sharing options...
trq Posted March 12, 2007 Share Posted March 12, 2007 From the manual. Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value. Using it the way you want will be unreliable. What exactly are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/42348-solved-php-id/#findComment-205432 Share on other sites More sharing options...
Ninjakreborn Posted March 12, 2007 Author Share Posted March 12, 2007 I wanted a quicker way to retrieve the id, without doing it the long way. I am trying to do stuff lately that cuts down on time. Originally you insert something into the database, then you sometimes need to get the id for various purposes. In this situation, I need to enter the data, get the id and have it available for the review form. I wanted to see (since I end up needing to do that a lot) if there was a better way than a full select statement, query, and variable. I always do <?php $selectid = "SELECT id FROM table WHERE whatever here to show it's the right one;"; $queryid = mysql_query($selectid); if ($rowid = mysql_fetch_array($queryid)) { $id = $rowid['id']; } ?> This is something I do quite often. So in essence, if I want the id retrieval to work on that one. I will probably end up having to do it the long way, if so Oh well, but I saw about this id. I had someone on here recommend me to go ahead and use that instead, but if it's unreliable, I am not taking any chances. Quote Link to comment https://forums.phpfreaks.com/topic/42348-solved-php-id/#findComment-205445 Share on other sites More sharing options...
Ninjakreborn Posted March 12, 2007 Author Share Posted March 12, 2007 Well it actually takes an insert handler, so in theory if you provide that handler, with the resource of the insert, then it is infallible, right? Quote Link to comment https://forums.phpfreaks.com/topic/42348-solved-php-id/#findComment-205462 Share on other sites More sharing options...
trq Posted March 12, 2007 Share Posted March 12, 2007 If your planning on using it straight after the insert (its intended purpose) you'll be fine. Quote Link to comment https://forums.phpfreaks.com/topic/42348-solved-php-id/#findComment-205465 Share on other sites More sharing options...
Ninjakreborn Posted March 12, 2007 Author Share Posted March 12, 2007 Perfect, that's what I meant, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/42348-solved-php-id/#findComment-205497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.