supanoob Posted September 6, 2007 Share Posted September 6, 2007 ok so i have the following insert statement and i would like to know how to get the unique auto increment ID from the row it inserts straight after inserting it. So after inserting it i would like to get the "battle_id" field from the row just inserted, is it possible? <?php $query="insert into battle_log (defender_id, attacker_id, attacker_ip, defender_ip, attacker_start_health, defender_start_health, battle_time, battle_date) values ('$attack', '$account_id', '$ip_1', '$ip_2', '$health_1', '$health_2', NOW(), NOW())"; $result=mysql_query($query); ?> this thing is i cannot use the player ID's incase they have battled before it would bring multiple results, i need it to bring only 1. Quote Link to comment https://forums.phpfreaks.com/topic/68220-another-quick-question/ Share on other sites More sharing options...
Yesideez Posted September 6, 2007 Share Posted September 6, 2007 If you call mysql_insert_id() straight after INSERTing then it'll return the ID of what was just inserted. Quote Link to comment https://forums.phpfreaks.com/topic/68220-another-quick-question/#findComment-342954 Share on other sites More sharing options...
supanoob Posted September 6, 2007 Author Share Posted September 6, 2007 so would that go after the query but before result? Quote Link to comment https://forums.phpfreaks.com/topic/68220-another-quick-question/#findComment-342974 Share on other sites More sharing options...
supanoob Posted September 6, 2007 Author Share Posted September 6, 2007 i tried that but it didnt work, this is what i did: <?php $query="insert into battle_log (battle_id, defender_id, attacker_id, attacker_ip, defender_ip, attacker_start_health, defender_start_health, battle_time, battle_date) values ('', '$attack', '$account_id', '$ip_1', '$ip_2', '$health_1', '$health_2', NOW(), NOW())"; $result=mysql_query($query); $battle_id = mysql_insert_id(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/68220-another-quick-question/#findComment-343002 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.