|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
can i put last auto increment id into a variable?
Hello all. I'm new to php/mysql and I'm having problems. I've searched everywhere it seems, and to no avail.
Right after inserting into the db table, I need to figure out a way to get the last auto incremented id and put it into a php variable. My reason for this is that I want to concatenate the last id onto a url for a link to follow in an auto email. Here's what I've got and where I'm stumped: <?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); $sql = "INSERT INTO person (first,last,city,state,workplace,email,comments) VALUES ('$first','$last','$city','$state','$workplace','$ email','$comments')"; // run SQL against the DB $result = mysql_query($sql); // this is where the problem begins... $lastid = mysql_query("SELECT id FROM person WHERE email=$email",$db); // end of problem (note: i've tried using last_insert_id with no success--it always returns zero) $send_from_email = "me@mywebsite.com" ; $send_from_name = "Me" ; $subject = "Thank you" ; $mailto = $email ; $messageproper = "http://www.mywebsite.com/find.php3?id=" . $lastid ; mail($mailto, $subject, $messageproper, "From: "$send_from_name" <$send_from_email>nReply-To: "$send_from_name" <$send_from_email>nX-Mailer: chfeedback.php 2.0" ); } |
|
#2
|
|||
|
|||
|
RE: can i put last auto increment id into a variable?
There is MySQL function in PHP itself to achieve this.. mysql_insert_id() function returns the ID generated for an AUTO_INCREMENT field by the previous INSERT query.
$lastId=mysql_insert_id(); Use the above line in your problem area |
|
#3
|
|||
|
|||
|
RE: can i put last auto increment id into a variable?
Thanks so much Nazly! I'd say that Allman quote suits you well! I appreciate the help.
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > can i put last auto increment id into a variable? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|