Jump to content

Recommended Posts

How exactly can I use Start Transaction? I've seen numerous examples on the Internet, but they confuse me. They have different syntaxes...

 

All I want to do is have an Infirmary for my RPG.

 

Member 1 starts with 40 health.

 

They take damage and have 25 health.

 

They go to the infirmary and get rejuvenated to 40.

 

Members will have different healths, so I can't just define a variable to restore them to their original health, so someone told me to use START TRANSACTION as an SQL statement..

 

Help pleeeeeease. :):D

Link to comment
https://forums.phpfreaks.com/topic/172233-using-start-transaction/
Share on other sites

Wrong. You only need a simple update statement. If you would use transactions you would be able to rollback so if my character would take damage it would automatically heal if you would rollback.

 

START TRANSACTION;

UPDATE characters SET health = 25 WHERE id = $id;//25 HP

ROLLBACK;//40 HP

Start Transaction is used when you are doing several updates to a database in one go (or transaction).

 

Consider character A selling a dagger to character B for 20 gp: there are several updates/inserts/deletes.

INSERT INTO inventory (character, item) VALUES ('B','dagger');
DELETE FROM inventory WHERE character = 'A' and item = 'dagger';
UPDATE characters SET money=money-20 WHERE character = 'B'
UPDATE characters SET money=money+20 WHERE character = 'A'

If, for some reason, the script fails after the INSERT but before the DELETE, then both character A and character B will have a dagger, and both will still have the same amount of money as when they started... not what you want to happen.

 

If you wrap this in a transaction, then:

START TRANSACTION
INSERT INTO inventory (character, item) VALUES ('B','dagger');
if (error) { ROLLBACK; exit; }
DELETE FROM inventory WHERE character = 'A' and item = 'dagger';
if (error) { ROLLBACK; exit; }
UPDATE characters SET money=money-20 WHERE character = 'B'
if (error) { ROLLBACK; exit; }
UPDATE characters SET money=money+20 WHERE character = 'A'
if (error) { ROLLBACK; exit; }
COMMIT

Now, if the script fails after the INSERT but before the DELETE, then the code will rollback, meaning that it will reverse the insert that succeeded before it exits, so the database will not contain a new dagger record in inventory for character B.

Only if all four statements work correctly will the database changes be "committed" (made permanent).

 

Oh, okay, well that makes a whole lot more sense.  :D

 

@ignace, if each character has different health, say...40, 55, 100, and 60, how would I determine their starting score to rejuvenate only that much? If I wanted to heal #1 with 40hp, and they had 60, I would add 20, but then that would add 20 to everyone that heals. I just want to heal them back to their starting score. :)

@ignace, if each character has different health, say...40, 55, 100, and 60, how would I determine their starting score to rejuvenate only that much? If I wanted to heal #1 with 40hp, and they had 60, I would add 20, but then that would add 20 to everyone that heals. I just want to heal them back to their starting score.

So you must have a record of their starting score somewhere, right?

@ignace, if each character has different health, say...40, 55, 100, and 60, how would I determine their starting score to rejuvenate only that much? If I wanted to heal #1 with 40hp, and they had 60, I would add 20, but then that would add 20 to everyone that heals. I just want to heal them back to their starting score.

So you must have a record of their starting score somewhere, right?

 

You prolly associate (max) health with the characters level, right? Thus:

 

CREATE TABLE levels (
    levels_id SMALLINT NOT NULL AUTO_INCREMENT,
    levels_max_health SMALLINT,
    PRIMARY KEY (levels_id)
);

 

Use the value of levels_max_health to rejuvenate it.

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.