Jump to content

Selected not sticking to variable?


Douglas Rhiner

Recommended Posts

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
Share on other sites

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
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.