Assume that the websites table has an automatically generated ID field. Return the ID from the last query:
<?php // Assume database username: root, password: 123456, database: CODERCTO $con=mysqli_connect("localhost","root","123456","CODERCTO"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_query($con,"INSERT INTO websites (name,url,alexa) VALUES ('Coder Tutorial','http://www.codercto.com',5633)");//Output the automatically generated IDecho "New The id is: " . mysqli_insert_id($con); mysqli_close($con);?>
The mysqli_insert_id() function returns the automatically generated ID (generated via AUTO_INCREMENT) from the last query.
mysqli_insert_id( connection ) ;
parameter | describe |
---|---|
connection | Required. Specifies the MySQL connection to use. |
Return value: | Returns an automatically generated integer with the AUTO_INCREMENT field value in the last query. If number > maximum integer value, it will return a string. If there is no update or no AUTO_INCREMENT field, 0 will be returned. |
---|---|
PHP version: | 5+ |