Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo id when id is auto increment?

Tags:

php

mysql

I can't seem to find this. Maybe I'm asking the wrong question. My first field in my db is an id with INT set to auto increment. When I create a new entry I want to grab the id that was created, but I don't know how.

$query = mysqli_query($myConnection, "INSERT INTO books (title, author, description) 

VALUES('$title','$author','$description')") 

or die (mysqli_error($myConnection));



echo 'Operation Completed Successfully! <br /><br /><a href="index.php">Click Here</a><br /><br />';


//HERE I WOULD ALSO LIKE TO ECHO THE ID THAT WAS CREATE WITH AUTO INCREMENT
echo $title . "<br />" . $author . "<br /> . $description;
like image 838
user2816376 Avatar asked Feb 19 '26 14:02

user2816376


2 Answers

Try this:

echo "The last id was: " . msqli_last_insert_id();
like image 131
arielcr Avatar answered Feb 22 '26 05:02

arielcr


Use this function: mysqli_insert_id()

like image 32
Pupil Avatar answered Feb 22 '26 04:02

Pupil