Jump to content

Help the newbie? Can't get my MySQL_query right.


Bikkebakke

Recommended Posts

So, I'm new to PHP/MySQL, I've been reading a lot of tutorials and got my own domain and (PHP enabled) webhost and MySQL server.

 

I'm kind of learning the language with testing simple stuff, my ultimate goal is to create a browser-game but that's not going to happen anytime soon  8)

 

The problem is I've created a "battle" page, theres no battle yet but I'm trying to code it so that when you go to the battle page it "finds and slays a monster" and you gain exp and gold. As I said, I'm just testing stuff and figuring out how PHP/MySQL works.

(Create own table and make the PHP increase the value of a field)

 

I don't seem to get what am I missing or is there a typo here:

 

$get->exp = rand(5,50);
$get->gold = rand(1,30);

mysql_query("UPDATE users SET exp = exp + $get->exp, gold = gold + $get->gold".$_SESSION["user_id"]."'");

 

echo "<p><strong>Outcome:<br></strong>";
echo "You killed the monster and gained $get->Exp experience points and $get->Gold gold!";

 

It doesn't update the db table and it just shows the following in browser:

Outcome:

You killed the monster and gained experience points and Zeny!

 

 

I'd appreciate it if someone points out what am I missing :o

Link to comment
Share on other sites

You'll need to enclose $get->whatever in curly brackets, and your $_SESSION['user_id'] seems to be used in the wrong place (plus the double-quoted user_id conflicts with the double quotes used to hold the query). Try the following:

 

mysql_query("UPDATE users SET exp = exp + {$get->exp}, gold = gold + {$get->gold} WHERE id = {$_SESSION['user_id']}");

 

And as an extra note, your method of returning the properties of a class is not the preferred. The better way is to call something similar to $object->GetGold() (or, maybe, $object->Get('gold')).

Link to comment
Share on other sites

Alright, I'll keep that in mind, thanks :3

 

Now that I got that thing working I'll work on making a simple battle system:

Get player hp/atk/def, get monster hp/atk/def, player damage is player atk (minus) monster def and vice versa.

This repeats till either player or monster hp is <=0.

 

I'd be happy if someone could point out where to start coding this  ;D

Link to comment
Share on other sites

since you seem to be using OOP to some extent, storing the hp/atk/def of players and monsters, and retrieving them should be pretty straight forward. A battle loop would be pretty simple (based on how simple your description is), and if its just based on atk and def (not weapons or special status or anything, something as simple as)

while($player->health > 0 && $monster->health > 0){
//do battle
}

 

could work. you may want to tweak the logic a little if you want extra stuff

Link to comment
Share on other sites

Yea, I'll just be doing simple stuff now and add extra content as I get more experienced.

 

I'm not concerned about typing a code that forbids me from adding certain vital content (whatever it could be) cause of how the core is built, cause I'm well aware I'll most likely have to re-code everything at some point.

 

I had thought of something like a while loop and I'm pretty sure I can code it but,

Getting player hp and deducting the damage taken from it sounds rather easy but I have to get the monster hp differently.

(Since monsters 'stats' should remain unmodified but players stats should change as the player develops)

 

Would it be stupid to make a value that increases as the player "inflicts damage" and once that value gets >=monster hp, the battle is over and player wins?

And when monster attacks the players hp value in db table is reduced and if/when it reaches <=0 the battle is over and player lost and transferred to "hospital" or something.

Link to comment
Share on other sites

well, i don't see why simply subtracting from a monster objects health would be bad. monsters are fairly static, but each seperate monster object is just as dynamic as the player (well not quite as dynamic, but their health would go down) If the player wants to fight another monster, you can just instantiate a new monster object.

 

however, what you proposed wouldn't be bad. but displaying the monsters remaining health would involve an extra calculation (which isn't bad, just relatively unnecessary). As far as updating the db table with the players health, I personally would only update the db after a battle is finished, to reduce the amount of queries that are run in a players game session, but that is more of a stylistic choice. it may benefit your server if you do it this way if you get a lot of traffic however.

Link to comment
Share on other sites

But if I deduct the monsters hp as the player damages it and another player fights same type monster, wouldn't the other player fight a "weakened" monster as it's hp was lower due to the first player attacking it?

 

Wouldn't it be harder to code it so that when a player fights a monster, it creates an instance of the monster based on the stats given in the database rather than just counting the damage dealt and checking if it has reached the monsters hp?

 

Also, I have no idea where to start coding the turns of the battle.. o_O

 

Link to comment
Share on other sites

for one, the monster that one player fights isn't the same as another. that simply isn't how objects work. Not to mention that the script running for one player won't be using the information from another. creating a new instance of an object is one line (ex. $monster = new Monster())

 

I can't really give a concrete answer without seeing the class though.

 

as far as one way being harder than the other, the differences in code is rather negligible. One way isn't any harder from another, semantically, but if you want to do it the OOP way, than you should create a monster with stats from the DB rather than just comparing the amount of damage that a player does to the amount of damage a monster can take. What if you want to update the battle function to take other things into account (like armor value?) .

 

the turns of the battle would be pretty easy. something like

while(..){
$monster->health -= $player->atk;
//or
$plyDmg += $player->atk;

$player->health -= $monster->atk;
}

 

if you wanted to take advantage of encapsulation and other OOP principles (basically do OOP) then something like

$player->fight($monster);

may be what your after

 

the way I personally would do it, is after I created the fight method (which does one round of fighting, not the whole battle) have 2 methods called dead (which return whether or not the player/monster has 0 health) and have my loop logic be something like

while(!$player->dead() && !$monster->dead())

 

 

 

Link to comment
Share on other sites

Thanks for clearing this out, I think I got most of the knowledge I need for this step.

 

I'm still new to PHP and therefor don't know what are the options when coding something (as there usually is multiple ways of doing things) cause I don't know ANY way of doing something before searching and finding it out.

 

I'll start coding and ask for help if I encounter any problems too big for me to figure out.

 

Once again, thanks a lot mikesta707!  ;)

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.