Saturday 1 September 2018

mysql - How can I get the current ID number with INSERT INTO?











How can I do get current ID number with INSERT INTO?



mysql_query("INSERT INTO mytable(id,a,b) VALUES(null,3,current ID) ");

mysql_query("
INSERT INTO tablo(id,a,b)
VALUES(null,3,this ID number)
^ ^

| |
| |
|____________|
");

Answer



Use mysqli_insert_id(), which returns the auto-generated id used in the last query.



Read "PHP mysql_insert_id() Function" or "mysql_insert_id".




Example:



$query= "INSERT INTO table_name VALUES ('a','b','c')";
$result = mysql_query($query);
echo "INSERT statement ID is" . mysql_insert_id();
?>


And do read the big red box in the PHP documentation on the mysql_insert_id page, starting with mysqli.




See the PHP Documentation For further reference.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...