coupe-r Posted February 4, 2011 Share Posted February 4, 2011 Hi All, Currently, I am doing the following: 1. Inserting data into my Property table -- (INSERT INTO ...) 2. After inserting, I am grabbing the newly created record ID with mysqli_insert_id ($new_prop_id = $mysqli_insert_id($connect); 3. Inserting more data into a different table, including the $new_prop_id. My question is, is this a safe way of doing it? Will I always grab the last ID of this specific user? My alternative would be doing a SELECT instead of the insert_id(). Suggestions? Link to comment https://forums.phpfreaks.com/topic/226628-mysqli_insert_id-question/ Share on other sites More sharing options...
DarkKnight2011 Posted February 4, 2011 Share Posted February 4, 2011 i have always used... $lastInsert = mysql_insert_id(); just after inserting a new row and found it very reliable to use, if you did want to do a query to make sure you could do this.. select max(id) from table but i have never needed to.. has always been accurate Hope this helps also some information on the mysql manual page... $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->query("CREATE TABLE myCity LIKE City"); $query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)"; $mysqli->query($query); printf ("New Record has id %d.\n", $mysqli->insert_id); Link to comment https://forums.phpfreaks.com/topic/226628-mysqli_insert_id-question/#findComment-1169813 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.