Re: [PHP-DB] grabbing id from new record
Hi. What about Auto_increemnt field ?? Your's id can be auto_increment field. for example: create table dupencja (id int(7) auto_increment not null, primary key(id)); so when you insert records: insert into dupencja values('') - id will be 1 insert into dupencja values('') - id will be 2 this is auto_increment, so your trick isn't need. More in manula of mysql. regards. I piasecki - www.b-c.pl - Original Message - From: George Pitcher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 3:23 PM Subject: [PHP-DB] grabbing id from new record Hi, Is there a way to grab the ID (from ID field) while INSERTing a new record? I am currently doing a separate INSERT then a SELECT query to find the most recently added record by that user: select ID from table where user = '$user' order by ID DESC limit 0,1 This does the trick but it may not be the most efficient way. George --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Znudzilo Ci sie logo w komorce? Wgraj nowe [ http://komorki.onet.pl/dodatki.html ] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] grabbing id from new record
I am sorry, I was not clear in my original request. The id is auto-incremented. I want to grab the id so that I can use it as part of a redirect $q2=select Course_ID from course where HEI_ID = '$inst' order by Course_ID desc limit 0,1; $result2=mysql_query($q2) or die(q2 failed); while ($row = mysql_fetch_array ($result2)) { $recid=$row['Course_ID']; } $redirect=('Location: http://localhost/Mysite/itemlist.php?recid=' . $recid); header($redirect); As I said, it works but maybe there's a better way. George What about Auto_increemnt field ?? Your's id can be auto_increment field. for example: create table dupencja (id int(7) auto_increment not null, primary key(id)); so when you insert records: insert into dupencja values('') - id will be 1 insert into dupencja values('') - id will be 2 this is auto_increment, so your trick isn't need. More in manula of mysql. regards. I piasecki - www.b-c.pl - Original Message - From: George Pitcher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 3:23 PM Subject: [PHP-DB] grabbing id from new record Hi, Is there a way to grab the ID (from ID field) while INSERTing a new record? I am currently doing a separate INSERT then a SELECT query to find the most recently added record by that user: select ID from table where user = '$user' order by ID DESC limit 0,1 This does the trick but it may not be the most efficient way. George --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Znudzilo Ci sie logo w komorce? Wgraj nowe [ http://komorki.onet.pl/dodatki.html ] --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] grabbing id from new record
straight after you do the insert command, use the call: $ID = mysql_insert_id(); The $ID is now the primary key for the record you just inserted. -Original Message- From: George Pitcher [mailto:[EMAIL PROTECTED]] Sent: 14 January 2002 14:56 To: Ireneusz Piasecki; [EMAIL PROTECTED] Subject: Re: [PHP-DB] grabbing id from new record I am sorry, I was not clear in my original request. The id is auto-incremented. I want to grab the id so that I can use it as part of a redirect $q2=select Course_ID from course where HEI_ID = '$inst' order by Course_ID desc limit 0,1; $result2=mysql_query($q2) or die(q2 failed); while ($row = mysql_fetch_array ($result2)) { $recid=$row['Course_ID']; } $redirect=('Location: http://localhost/Mysite/itemlist.php?recid=' . $recid); header($redirect); As I said, it works but maybe there's a better way. George What about Auto_increemnt field ?? Your's id can be auto_increment field. for example: create table dupencja (id int(7) auto_increment not null, primary key(id)); so when you insert records: insert into dupencja values('') - id will be 1 insert into dupencja values('') - id will be 2 this is auto_increment, so your trick isn't need. More in manula of mysql. regards. I piasecki - www.b-c.pl - Original Message - From: George Pitcher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 3:23 PM Subject: [PHP-DB] grabbing id from new record Hi, Is there a way to grab the ID (from ID field) while INSERTing a new record? I am currently doing a separate INSERT then a SELECT query to find the most recently added record by that user: select ID from table where user = '$user' order by ID DESC limit 0,1 This does the trick but it may not be the most efficient way. George --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Znudzilo Ci sie logo w komorce? Wgraj nowe [ http://komorki.onet.pl/dodatki.html ] --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.313 / Virus Database: 174 - Release Date: 02/01/02 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.313 / Virus Database: 174 - Release Date: 02/01/02 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] grabbing id from new record
Thanks, just what I was looking for. George - Original Message - From: matt stewart [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 3:26 PM Subject: RE: [PHP-DB] grabbing id from new record straight after you do the insert command, use the call: $ID = mysql_insert_id(); The $ID is now the primary key for the record you just inserted. -Original Message- From: George Pitcher [mailto:[EMAIL PROTECTED]] Sent: 14 January 2002 14:56 To: Ireneusz Piasecki; [EMAIL PROTECTED] Subject: Re: [PHP-DB] grabbing id from new record I am sorry, I was not clear in my original request. The id is auto-incremented. I want to grab the id so that I can use it as part of a redirect $q2=select Course_ID from course where HEI_ID = '$inst' order by Course_ID desc limit 0,1; $result2=mysql_query($q2) or die(q2 failed); while ($row = mysql_fetch_array ($result2)) { $recid=$row['Course_ID']; } $redirect=('Location: http://localhost/Mysite/itemlist.php?recid=' . $recid); header($redirect); As I said, it works but maybe there's a better way. George What about Auto_increemnt field ?? Your's id can be auto_increment field. for example: create table dupencja (id int(7) auto_increment not null, primary key(id)); so when you insert records: insert into dupencja values('') - id will be 1 insert into dupencja values('') - id will be 2 this is auto_increment, so your trick isn't need. More in manula of mysql. regards. I piasecki - www.b-c.pl - Original Message - From: George Pitcher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 3:23 PM Subject: [PHP-DB] grabbing id from new record Hi, Is there a way to grab the ID (from ID field) while INSERTing a new record? I am currently doing a separate INSERT then a SELECT query to find the most recently added record by that user: select ID from table where user = '$user' order by ID DESC limit 0,1 This does the trick but it may not be the most efficient way. George --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Znudzilo Ci sie logo w komorce? Wgraj nowe [ http://komorki.onet.pl/dodatki.html ] --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.313 / Virus Database: 174 - Release Date: 02/01/02 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.313 / Virus Database: 174 - Release Date: 02/01/02 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] grabbing id from new record
That actually isn't a safe way to do it. Consider the following scenario: Insert A occurs generating row Y in your table. Insert B occurs generating row Z in your table. select occurs (after insert A) to grab last ID inserted...your result is the ID from row Z. select occurs (after insert B), and your result again is the result ID from row Z. So, in both cases you will get the ID from insert B, instead of your intended result (first select gives the ID from row Y and the second from row Z). You didn't mention which database engine that you are using, so I will assume you are using MySQL. There is a function called mysql_insert_id() that will give you the answer that you are looking for. You can find more information about it here: http://www.php.net/manual/en/function.mysql-insert-id.php HTH Sam Masiello Software Quality Assurance Engineer Synacor (716) 853-1362 X289 [EMAIL PROTECTED] - Original Message - From: George Pitcher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 9:23 AM Subject: [PHP-DB] grabbing id from new record Hi, Is there a way to grab the ID (from ID field) while INSERTing a new record? I am currently doing a separate INSERT then a SELECT query to find the most recently added record by that user: select ID from table where user = '$user' order by ID DESC limit 0,1 This does the trick but it may not be the most efficient way. George --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] grabbing id from new record
Sam, Thanks, I got the mysql_insert_id() tip a few seconds earlier. However, my alternative method (which I will now drop in the interests of efficiency) is perfectly safe. It is impossible for the same user to go and create another record in that verys small interim period between the two queries. That was why the WHERE user='$user' was there. George - Original Message - From: Sam Masiello [EMAIL PROTECTED] To: George Pitcher [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Monday, January 14, 2002 3:21 PM Subject: Re: [PHP-DB] grabbing id from new record That actually isn't a safe way to do it. Consider the following scenario: Insert A occurs generating row Y in your table. Insert B occurs generating row Z in your table. select occurs (after insert A) to grab last ID inserted...your result is the ID from row Z. select occurs (after insert B), and your result again is the result ID from row Z. So, in both cases you will get the ID from insert B, instead of your intended result (first select gives the ID from row Y and the second from row Z). You didn't mention which database engine that you are using, so I will assume you are using MySQL. There is a function called mysql_insert_id() that will give you the answer that you are looking for. You can find more information about it here: http://www.php.net/manual/en/function.mysql-insert-id.php HTH Sam Masiello Software Quality Assurance Engineer Synacor (716) 853-1362 X289 [EMAIL PROTECTED] - Original Message - From: George Pitcher [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, January 14, 2002 9:23 AM Subject: [PHP-DB] grabbing id from new record Hi, Is there a way to grab the ID (from ID field) while INSERTing a new record? I am currently doing a separate INSERT then a SELECT query to find the most recently added record by that user: select ID from table where user = '$user' order by ID DESC limit 0,1 This does the trick but it may not be the most efficient way. George --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.307 / Virus Database: 168 - Release Date: 11/12/01 _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]