blue-genie Posted September 14, 2009 Share Posted September 14, 2009 what i'm doing is before I insert a new row, i want to get the last value in the gameInstance_No field, increment that by 1 and do the insert. while I run the select statement it returns the value correctly however the insert is putting a zero. $sql = "SELECT gameInstance_No FROM gameinstance ORDER BY gameInstance_No DESC LIMIT 0,1"; $result = mysql_query($sql); if (mysql_num_rows($result)){ $gameInstanceNo = $row['gameInstance_No']+1; }else{ $gameInstanceNo = 0; } thanks. Link to comment https://forums.phpfreaks.com/topic/174159-solved-need-to-increment-a-number/ Share on other sites More sharing options...
trq Posted September 14, 2009 Share Posted September 14, 2009 Why don't you let mysql handle this itself? You method isn't at all reliable. Link to comment https://forums.phpfreaks.com/topic/174159-solved-need-to-increment-a-number/#findComment-918080 Share on other sites More sharing options...
blue-genie Posted September 14, 2009 Author Share Posted September 14, 2009 oh sorry i didn't refresh my page, didn't see your response. what do you mean by let sql handle this? auto increment? Link to comment https://forums.phpfreaks.com/topic/174159-solved-need-to-increment-a-number/#findComment-918086 Share on other sites More sharing options...
blue-genie Posted September 14, 2009 Author Share Posted September 14, 2009 this is what I opted for, not sure if it's the best way. <?php session_start(); include 'config.php'; include 'opendb.php'; $gameID= $_REQUEST['gameID']; $gameRNG= $_REQUEST['gameRNG']; $insert = mysql_query("INSERT INTO gameinstance (gameID, gameRNGResult, gameDateTime) VALUES ('$gameID', '$gameRNG', now())"); if(!$insert){ echo '<?xml version="1.0"?>'; echo '<dataxml>'; die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; echo '</dataxml>'; } else { $gameInstanceNo = mysql_insert_id(); printf("Last inserted record has id %d\n", mysql_insert_id()); mysql_query("UPDATE gameInstance SET gameInstance_No='$gameInstanceNo' WHERE USN_Gameinstance='$gameInstanceNo'"); echo '<?xml version="1.0"?>'; echo '<dataxml>'; $success = mysql_insert_id(); echo "<success>".$success."</success>"; echo '</dataxml>'; } ?> Link to comment https://forums.phpfreaks.com/topic/174159-solved-need-to-increment-a-number/#findComment-918098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.