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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.