Bikkebakke Posted February 9, 2010 Share Posted February 9, 2010 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/ Share on other sites More sharing options...
MRushton Posted February 9, 2010 Share Posted February 9, 2010 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')). Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009463 Share on other sites More sharing options...
Bikkebakke Posted February 9, 2010 Author Share Posted February 9, 2010 Thanks! It works now! I noticed I should use single-quote ' intead of " but thanks for the whole corrected line! I don't remember reading anything about having to use curly brackets tho' I'll try to remember it in the future. Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009468 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 you also want to use curly brackets when using arrays in strings, like $arr = array(....);//some array $str = "i have {$arr['key1']} cars, but don't have {$arr[0]} bugs"; Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009472 Share on other sites More sharing options...
Bikkebakke Posted February 9, 2010 Author Share Posted February 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009487 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009492 Share on other sites More sharing options...
Bikkebakke Posted February 9, 2010 Author Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009508 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009513 Share on other sites More sharing options...
Bikkebakke Posted February 9, 2010 Author Share Posted February 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009525 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 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()) Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009536 Share on other sites More sharing options...
Bikkebakke Posted February 9, 2010 Author Share Posted February 9, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009545 Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 no problem. if your topic is solved you can click the mark solved button at the bottom of the thread Quote Link to comment https://forums.phpfreaks.com/topic/191495-help-the-newbie-cant-get-my-mysql_query-right/#findComment-1009547 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.