zelig Posted October 30, 2011 Share Posted October 30, 2011 Here's what I have, before I paste the code: I want to update 3 fields within a table every minute using a cron job. I want it to give them 3 energy/mp/hp renewed per minute as long as that user's state is 'rest'. However, I can't seem to get it to do that... I've worked on this for a while, and banging my head against a wall. Any help would be MUCH appreciated!! Thanks! <?php include('../lib.php'); $player = check_user($db, $secret_key); $query = $db->execute("select * from `users` where `id`=?", $player->id); echo $player->username; if($player->state == 'rest') if($player->energy === maxenergy && $player->hp === maxhp && $player->mp ===maxmp && $player->state ==='rest') { $query = $db->execute("update `users` set `state`='getup' where `id`=?", array($player->id)); echo "You have full hp, energy, and mp!"; } else { $query1 = $db->execute("update `users` set `hp`=?, `mp`=?, `energy`=? where `id`=?",array($player->hp + 3, $player->mp + 3, $player->energy + 3, $player->id)); } ?> Quote Link to comment Share on other sites More sharing options...
creata.physics Posted October 30, 2011 Share Posted October 30, 2011 $query1 = $db->execute("update `users` set `hp`=?, `mp`=?, `energy`=? where `id`=?",array($player->hp + 3, $player->mp + 3, $player->energy + 3, $player->id)); I'll help fix your question marks $query1 = $db->execute("update `users` set `hp`= hp+3, `mp`=mp+3, `energy`=energy+3 where `id`=?",array($player->hp + 3, $player->mp + 3, $player->energy + 3, $player->id)); That query increases current hp, mp and energy by 3 everytime that part of the script is ran. You may have to take off your array, I'm not sure since I have no idea exactly what execute() does. Quote Link to comment Share on other sites More sharing options...
zelig Posted October 30, 2011 Author Share Posted October 30, 2011 Hmm.... still not updating. A quick side note, is there a way to make it so that it doesn't go over their "max" stats? (maxhp, maxmp, maxenergy) Quote Link to comment Share on other sites More sharing options...
creata.physics Posted October 30, 2011 Share Posted October 30, 2011 What is the query error you're getting? There is a way to make it so the stats won't go above 100, but that seems a tiny bit less important than getting the query to work. Because if the query wont work, it won't matter since it won't get close to 100. I need to understand how $db->execute() works. I also need to know what query error you get since your query is not updating. Also, the code I gave you shouldn't exactly work, since id is left with a ?, that may be how your code works. I'd try a simple update with just mysql_query. $query1 = mysql_query("update users set `hp`= hp+3, `mp`= mp+3, `energy`= energy+3 where `id`= '{$player->id}'"); if ( ! $query1 ) { die(mysql_error()); } Quote Link to comment Share on other sites More sharing options...
zelig Posted October 30, 2011 Author Share Posted October 30, 2011 No error pops up, so it appears it is working. It's just not updating the stats. Quote Link to comment Share on other sites More sharing options...
creata.physics Posted October 31, 2011 Share Posted October 31, 2011 Please manually enter the query in phpmyadmin, you will need to replace $player->id manually with an actual users id. Run the query in phpmyadmin and post the results here. If the query is not getting an error, then it is running, it just may not be updating any specific information. I will need to see why to help any further. Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Hate to sound like a newbie, but how do I run the query in phpmyadmin? Quote Link to comment Share on other sites More sharing options...
creata.physics Posted October 31, 2011 Share Posted October 31, 2011 Not a problem, once you go to phpmyadmin and click the database, there will be a tab at the top that says sql. Once you click sql, you'll have a textarea that lets you run a query. You would usually access phpmyadmin via your cpanel that your host provides. Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Hmm... This is the query I used: update `users` set `hp`= hp+3, `mp`=mp+3, `energy`=energy+3 where `id`='testerzelig' Didn't work. No errors in the query came up. Quote Link to comment Share on other sites More sharing options...
seany123 Posted October 31, 2011 Share Posted October 31, 2011 Hmm... This is the query I used: update `users` set `hp`= hp+3, `mp`=mp+3, `energy`=energy+3 where `id`='testerzelig' Didn't work. No errors in the query came up. 1. i dont understand why you have ID as a varchar, it would be alot easier and more efficient to use a primary Auto increment Int 2. Could you please post the structure of the table, this can be done by going to phpmyadmin/database/table/export/go note - you might want to remove any sensitive data before posting. 3. back inside the phpmyadmin/sql box... does this return the correct row? SELECT * FROM `users` WHERE id='testerzelig' 4. Can you find out and post what these 2 functions actually do: $db->execute check_user im guessing it will be either inside lib.php or inside a filed included in lib.php 5. after echo $player->username; have echo $player->id; to see if it echos to correct id. Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 1. i dont understand why you have ID as a varchar, it would be alot easier and more efficient to use a primary Auto increment Int I want it to just find the characters that are in the state 'rest', so it searches by username (ID). The structure is huge (since it's in development, I've added things that won't make it into the actual game). But, here is the `users` table. (attached) 3. back inside the phpmyadmin/sql box... does this return the correct row? MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0004 sec ) 4. Can you find out and post what these 2 functions actually do: $db->execute check_user Check User Function function check_user($secret_key, &$db) { if (!isset($_SESSION['userid']) || !isset($_SESSION['hash'])) { exit; } else { $check = sha1($_SESSION['userid'] . $_SERVER['REMOTE_ADDR'] . $secret_key); if ($check != $_SESSION['hash']) { session_unset(); session_destroy(); exit; } else { $query = $db->execute("select * from `users` where `id`=?", array($_SESSION['userid'])); $userarray = $query->fetchrow(); if ($query->recordcount() == 0) { session_unset(); session_destroy(); exit; } foreach($userarray as $key=>$value) { $user->$key = $value; } return $user; } } } $db->execute I can't seem to find. I've use part of the code from a friend, and I'm waiting to hear back from him. Otherwise, I would assume all it does is execute the variables. 5. after echo $player->username; have echo $player->id; to see if it echos to correct id. Nothing echoed out onto the screen. Still just a blank white screen. I'm assuming, then, that it isn't returning anything... [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Hmm... if I use: update `users` set `hp`= hp+3, `mp`=mp+3, `energy`=energy+3 where `username`='Testerzelig' in mySQL, it works. I had to change `id` to `username` and 'testerzelig' to 'Testerzelig', but it worked just fine. So... something works, I just have to figure out what to do now... Quote Link to comment Share on other sites More sharing options...
seany123 Posted October 31, 2011 Share Posted October 31, 2011 It still doesnt make sense why you dont have id as an auto increment, this gives every new player a ID number which they cannot change and its how you should always query through the users, usernames are simply more messy. Basically we have found out that your trying to match your USERNAME with the database's ID which obviously doesnt match... so something like this should work: $query1 = mysql_query("update users set `hp`= hp+3, `mp`= mp+3, `energy`= energy+3 where `username`= 'Testerzelig'"); if ( ! $query1 ) { die(mysql_error()); } But did you say that nothing echos? because you have echo $player->username; on that page which should echo out your username: Testerzelig and $player->id should echo your players ID if this is a EZ code, then i would check you have downloaded the latest source because something code me wrong. Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Ah, yes, the ID # is an auto increment. Username is also used. My bad. Nothing echoes on the screen, correct. Just a blank white screen. And it used to be EZ code, like, years ago. It's been radically modified... Maybe that's where the $db->execute came from... Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 If I use Testerzelig's ID (341) in the `id`='341', it works fine too. So it appears that something is amiss in that it isn't searching correctly... Quote Link to comment Share on other sites More sharing options...
seany123 Posted October 31, 2011 Share Posted October 31, 2011 it looks like $player->id doesnt seem to be working, echo out $_SESSION['userid']; and see what that returns, its not making any sense because your logged in? Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Still just a blank white screen... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2011 Share Posted October 31, 2011 With all the exit()s you have in the code, it's no wonder you're getting a blank screen. Start putting in some descriptive debugging echos before each exit() so you can see just what the script is doing. If you don't have error_reporting = -1 and display errors = On in your php.ini file, set those 2 directives, restart Apache and see what, if any, errors are generated. There is no logic in that code to check for and deal with any database query errors. Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 I've got the reporting and display errors on, but nothing is still showing. What do you mean by all the exits? Where should I put the echoes at? I'm still learning, so I don't quite get what you mean... <?php include('../lib.php'); $player = check_user($db, $secret_key); $query = $db->execute("select * from `users` where `id`=?", $player->id); echo $player->username; echo $player->id; echo $_SESSION['userid']; if($player->state == 'rest') if($player->energy === maxenergy && $player->hp === maxhp && $player->mp ===maxmp && $player->state ==='rest') { $query = $db->execute("update `users` set `state`='getup' where `id`=?", array($player->id)); echo "You have full hp, energy, and mp!"; } else { $query1 = $db->execute("update `users` set `hp`= hp+3, `mp`=mp+3, `energy`=energy+3 where `id`=?",array($player->hp + 3, $player->mp + 3, $player->energy + 3, $player->id)); } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2011 Share Posted October 31, 2011 In the check_user function, there are at least 3 exit()s. Those really should return a value from the function to indicate what the failure was, but for the time being you can echo a description instead. Something like this: if (!isset($_SESSION['userid']) || !isset($_SESSION['hash'])) { echo 'Error: either $_SESSION['userid'] or $_SESSION['hash'] was not set.' // <--- descriptive message regarding error condition. exit; } Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Yeah, I got the error: Error: either $_SESSION[userid] or $_SESSION[hash] was not set. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2011 Share Posted October 31, 2011 Is there a call to session _start() in the script, before any output is sent to the browser, and before any $_SESSION vars are used? Where are the $_SESSION vars assigned their values initially? Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 The session starts in the lib.php document that is included. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2011 Share Posted October 31, 2011 Did you make sure the error reporting directives I mentioned earlier are set, and that Apache was restarted? Quote Link to comment Share on other sites More sharing options...
zelig Posted October 31, 2011 Author Share Posted October 31, 2011 Yup. Did all of that. display_errors = On register_globals=1 Quote Link to comment 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.