Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieving last auto _increment number

Status
Not open for further replies.

natefanaro

Technical User
Jun 10, 2002
17
US
I have seen the LAST_INSERT_ID() function but for some reason it either will not work for what I am looking to do, or I was using it wrong. I am trying to get the last number that is given by the auto_increment number in mysql. But when I look for info on LAST_INSERT_ID(), it's always used in another sql query. I am trying to do something similiar to this: $lastid = LAST_INSERT_ID()

Here's an example of my current code:

$db = mysql_connect("localhost", "quizcrea_root");

mysql_select_db("quizcrea_quiz",$db);

$sql = "INSERT INTO results (email,name,num1,num2,num3,bcolor,tcolor) VALUES ('$email','$name','$num1','$num2','$num3','$bcolor','$tcolor')";

$result = mysql_query($sql);

// I tried adding this but it gives an error


$lastid = LAST_INSERT_ID()

//end code

any suggestions?

 
I think the function you want is mysql_insert_id(). It will return the index of the recently inserted record. //Daniel
 
Thanks, just tried it but it returned a value of 0.

// begin

$lastid = mysql_insert_id();
echo $lastid;

// end

So it doesn't look like it worked. any other ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top