Jump to content

Can't seem to get this to update...


zelig

Recommended Posts

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));
    }
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));
    }
?>

Link to comment
Share on other sites

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;
}

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.