Jump to content

mysqli_insert_id question


coupe-r

Recommended Posts

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

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);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.