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
https://forums.phpfreaks.com/topic/84695-mysql_insert_id/
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
https://forums.phpfreaks.com/topic/84695-mysql_insert_id/#findComment-431646
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
https://forums.phpfreaks.com/topic/84695-mysql_insert_id/#findComment-431790
Share on other sites

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.