Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old January 29th, 2004, 11:36 PM
petey petey is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kansas
Posts: 2 petey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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" );


}

Reply With Quote
  #2  
Old January 30th, 2004, 12:21 AM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
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

Reply With Quote
  #3  
Old January 30th, 2004, 12:32 AM
petey petey is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kansas
Posts: 2 petey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > can i put last auto increment id into a variable?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway