Jump to content

mysql_insert_id()


TimUSA

Recommended Posts

I though I followed this right in the manual, but apparently not  ???

$raceresult = mysql_query("
SELECT mysql_insert_id()
FROM race_table
");
while ($row2 = mysql_fetch_row($raceresult)){
$raceID = $row2[0];

//enter information to the race_screenshots_table
mysql_real_escape_string($_POST['image']);
mysql_query("
INSERT INTO race_screenshots_table (imageURL, raceID)
VALUES ('{$_POST['image']}', '{$raceID}')");
}

Link to comment
Share on other sites

mysql_insert_id() can only be used AFTER an insert query is ran and you set it to a var like so

 

 

<?php

$query="SELECT * FROM tablea WHERE this=$that";
$result=mysql_query($query);

$lastID=mysql_insert_id(); /* you may need to pass the handle of the query var into this.. in my case here it would be mysql_insert_id($result);*/

echo 'The id of the last record inserted was '. $lastID;
?>

 

 

Link to comment
Share on other sites

As the value of the last insert id is "per connection" it has to made immediately after the insert was made. Once the script has finished the connection is closed and it is no longer available. This also ensures the it is the last id you inserted in your connection and not that of another user on another connection.

 

If you just have a SELECT as above, it is null.

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.