Douglas Rhiner Posted June 22, 2011 Share Posted June 22, 2011 I need another set of eyes and/or some real help. Been staring at this way toooooo long. Background: I have a scenario where I'm adding a "Location" and it's attributes to a database. From a form I capture among other things; Country, State, County, City, Region. The following PHP script is used to generate a master "object" ID for each "object"( Location, Country, State, County, City, Region) created. Assume all variables are passed to the script. $querya = "INSERT INTO vtindex (vtindex_type, vtindex_sesid) VALUES ('$type', '$sesid')"; $resulta = mysql_query($querya); if ($resulta) { $queryb = "SELECT * FROM `vtindex` WHERE `vtindex_sesid` = '$sesid' ORDER BY vtindex_time, vtindex_index DESC LIMIT 1"; $resultb = mysql_query($queryb); while($rowb = mysql_fetch_array($resultb, MYSQL_ASSOC)) { $newObjid = "{$rowb['vtindex_index']}"; } if ($resultb) {echo $newObjid;} if (!$resultb) {echo "Object Create - get objid error."; exit;} } The table `vtindex` has the structure: vtindex_index - int(11), UNSIGNED ZEROFILL, AUTO_INCREMENT, PRIMARY vtindex_type - text vtindex_time - timestamp, CURRENT_TIMESTAMP vtindex_sesid - text The MySQL version is 5.5.9 Brief synopsis: I create/INSERT a new row into `vtindex` via $querya. If success ( if ($resulta) ), I then immediately "get" the `vtindex_index` of the row just created with $queryb, as $newObjid. All seems good. However, if I require() this script more than once on a page, the value of $newObjid never changes. The insertion works as predicted every time, generating a new unique `vtindex_index`. But if I require() the script 6 times on a page ( to generate the 6 "objects" mentioned above ) the echo of $newObjid results in the same number all 6 times. For example: if the first value that is assigned to $newObjid is the number 00000000012, it will be echoed all six times. So from my perspective, what seems to be happening is that the value of $newObjid is not being updated/replaced with the new info SELECTED via $queryb the next time the script is "required()". Am I expecting unrealistic performance? Do I have the code wrong? Am I completely out in left field? Any help with this would be very much appreciated! Link to comment https://forums.phpfreaks.com/topic/240048-selected-not-sticking-to-variable/ Share on other sites More sharing options...
cags Posted June 22, 2011 Share Posted June 22, 2011 It sounds like you are simply trying to get the auto_increment value of the record you just inserted, if this is the case the mysql_insert_id will do this for you with a lot less effort (and more accuracy) than attempting it yourself. $querya = "INSERT INTO vtindex (vtindex_type, vtindex_sesid) VALUES ('$type', '$sesid')"; $resulta = mysql_query($querya); $newObjid = mysql_last_insert_id(); I could have misinterpreted, it's early. Link to comment https://forums.phpfreaks.com/topic/240048-selected-not-sticking-to-variable/#findComment-1233177 Share on other sites More sharing options...
Douglas Rhiner Posted June 22, 2011 Author Share Posted June 22, 2011 Whooo hooo! :D Thanks sooo much. I can sleep again! Link to comment https://forums.phpfreaks.com/topic/240048-selected-not-sticking-to-variable/#findComment-1233423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.