-2

I'm running a MySQL INSERT query where I insert some data into the database. Each row has a unique ID generated each time a new entry is added. I'd like to get this ID straight after I insert the data.

PHP Code:

$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color) 
    VALUES ('$orientation', '$title', '$color')") 
    or die(mysql_error());
//Here I need to get the row id of the above data...

Thanks!

NDM
  • 6,594
  • 3
  • 36
  • 50
Allen S
  • 3,351
  • 4
  • 31
  • 46

1 Answers1

4

Try like

$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color) 
VALUES ('$orientation', '$title', '$color')") 
or die(mysql_error());

echo "Inserted Id is ".mysql_insert_id();

Makesure that you have an auto increment field in your table.And also try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.

See this LINK

Gautam3164
  • 28,027
  • 10
  • 58
  • 83