SELECT LAST_INSERT_ID()

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

SELECT LAST_INSERT_ID()

Post by phpScott »

Arrrggghhhhhhh

I want to get the result back form this query

Code: Select all

$sql="SELECT LAST_INSERT_ID('sponId') FROM sponsor";
$r=mysql_query($sql);
I am doing error checking to make sure I get a result but I can't figure out how to access that result so I can play with the number that should be returned ex. 17 or what ever the last auto-incremented value is.

Any help is great, other suggestions to.

phpScott
MeOnTheW3
Forum Commoner
Posts: 48
Joined: Wed Nov 13, 2002 3:28 pm
Location: Calgary, AB

Post by MeOnTheW3 »

Get the manual at php.net


$last_auto_id = mysql_insert_id($result);
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Try something like:

Code: Select all

<?php
          $Query = "SELECT LAST_INSERT_ID()";
          $Result = mysql_query($Query, $Link);
          $row = mysql_fetch_array($Result);
          $last_id = $rowї0];  
?>
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Bill H

Post by phpScott »

thank you Bill H that is just what I needed. I knew that I was doing something silly with my code and sure enough it was.

phpScott
Post Reply