-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,731
  • 3
  • 39
  • 52
Allen S
  • 3,471
  • 4
  • 34
  • 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

GautamD31
  • 28,552
  • 10
  • 64
  • 85