Jump to content
xisto Community
Feelay

How To Make A Value In The Database Raise Every Minute.

Recommended Posts

Keep in mind that this entire method only works after you request the script. So, the script should be placed near the top of the master file for the system...

For example, if you use the usual index.php file as the traffic cop that controls the flow of data through your script, then somewhere near the beginning, after your configuration information, place either this code or an include for the file that this code is in.

The script should update everyone's hp when someone accesses the game and the proper amount of time has passed since the last update.

Okay, lets try and see if we missed something else. Some debugging could be in order:

Try something like this:

mysql_query("<<<INSERT YOUR QUERY HERE>>>") or die('Query failed: ' . mysql_error());
This is a good idea any time you are building a script since it prevents the system from continuing on and it lets you know that there is a propblem.

Another method we can use to check what is going on it by echoing the querys like so:
$query = "<<<INSERT YOUR QUERY HERE>>>";mysql_query($query) or die('Query failed: ' . mysql_error());echo $query;

If you want a fancier method of echoing your queries, you can concatenate (link together) all of your queies then output them at the end like so:
$some_count = 1;$query = "<<<INSERT YOUR QUERY HERE>>>";mysql_query($query) or die('Query failed: ' . mysql_error());$queries .= "$some_count:<br /><pre>$query</pre><br /><br />";$some_count++;....MORE CODE....echo $queries;
This would give you very easy to read query debug information.

Anyway, back to the main event.
It is possible that there is a problem with your PHP code that is breaking your queries in which you might see some strange data in your queries. If this is the case, then try and figure out which section of code generated that query and try and determine why your script is malfunctioning there.
It is also possible that the SQL queries that I have given you are bad! Since I didn't go to the trouble to create the required tables to do a live test with these queries, there could be an error. The error may not cause an error message to be shown if the query just doesn't actually match any of the records as it is written. So, you should copy and paste the queries into phpMyAdmin and manually test each one.

You may need to give us some database structure information so that we can see if there is something we are just overlooking.
Going into your database without selecting any tables to view, select Export and be sure to uncheck the Data checkbox and click Go. All of the other default settings will be fine and this will display the structure of all of your tables in that database. If you copy and paste that in your next reply, then I could see if there is something that I missed in your previous posts about your database. If I can't see it, I can actually use the export data to recreate the structure of your database on my account.

vujsa

Share this post


Link to post
Share on other sites
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";-- -- Database: `dbname`-- -- ---------------------------------------------------------- -- Table structure for table `characters`-- CREATE TABLE `characters` (  `user` varchar(32) NOT NULL,  `temphealth` varchar(225) NOT NULL default '100',  `attack` int(11) NOT NULL,  `exp` int(225) NOT NULL default '0',  `level` int(11) NOT NULL default '1',  `lastlogin` varchar(50) NOT NULL,  `maxhp` int(11) NOT NULL,  `expaw` int(11) NOT NULL,  `hptime` int(11) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Or maybe I've done something wrong.. I dunno, but here it is.

Share this post


Link to post
Share on other sites

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";-- -- Database: `dbname`-- -- ---------------------------------------------------------- -- Table structure for table `characters`-- CREATE TABLE `characters` (  `user` varchar(32) NOT NULL,  `temphealth` varchar(225) NOT NULL default '100',  `attack` int(11) NOT NULL,  `exp` int(225) NOT NULL default '0',  `level` int(11) NOT NULL default '1',  `lastlogin` varchar(50) NOT NULL,  `maxhp` int(11) NOT NULL,  `expaw` int(11) NOT NULL,  `hptime` int(11) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Or maybe I've done something wrong.. I dunno, but here it is.
Well, initially, I don't see a field for 'hp'. I don't know what you use the other fields for. Also, where are you storing the value for the last update? If you have a configuration table, that would be a good place to store your last update value or you can just create a new table just for that! Remember, the method I showed you will update every account at the same time and you only need to store one value for the last update time.

So, either you need to adjust your table to include the 'hp' field or modify the query to use the correct field where 'hp' currently resides.

So here is what I think needs to be done. Change 'hp' to 'temphealth' and 'user' to 'characters' in the queries:
$multiplier = 1 / 5;$frequency = 3600 * $multiplier;$increment = 5 * $multiplier;$query_count = 1;$query_debug = 1;  //  Set this to 0 to turn the query echo off!$current_time = time();  // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 0);  //  This could be anywhere even in a separate text file!$time_difference = $current_time - $last_update_time;$hours_passed = $time_difference / $frequency; //  convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour$full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour$remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0if($full_hours_passed > 0){	 $new_update_time = $current_time + $remaining_seconds;	 $hp_increase = $full_hours_passed * $increment;	 $query = "UPDATE characters SET temphealth = CASE WHEN temphealth + $hp_increase <= 100 THEN temphealth + $hp_increase ELSE 100 WHERE temphealth < 100";	 mysql_query($query) or die('Query failed: ' . mysql_error());	 $queries .= "$query_count:<br /><pre>$query</pre><br /><br />";	 $query_count++;	 $query = "UPDATE update_table SET update_time = $new_update_time";	 mysql_query($query) or die('Query failed: ' . mysql_error());	 $queries .= "$query_count:<br /><pre>$query</pre><br /><br />";	 $query_count++;}if($query_debug == 1){	 echo $queries;}

Please look at your queries! They have to match the data in your database. For example, have you created a table to file to hold the global value to last update time? You don't seem to need the field in the characters table named 'hptime'! Also, temphealth doesn't seem to need to be a varchar, I thin you should set this to int(11) since I think it should be a numeric value there. I coulld be wrong, I don't know how the rest of your system functions.

vujsa

Share this post


Link to post
Share on other sites

When you gave me the code, I changed the queries. I changed hp to temphealth. And i changed user to characters.the 'hptime' is the colum storing the last update. you arew right, temphealth should be int, but that don't matter. the value is still not changing =/

Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

When you gave me the code, I changed the queries. I changed hp to temphealth. And i changed user to characters.the 'hptime' is the colum storing the last update. you arew right, temphealth should be int, but that don't matter. the value is still not changing =/


;)
Well, this is getting a little frustrating!!!

First, does the query being used work?


Echo your queries as explained previously then plug those into phpMyAdmin and see it they work.
If they work in phpMyAdmin, then the trouble is in the code.
If they don't work, then we need to fix the queries.

Post the queries in your next reply.

vujsa

Share this post


Link to post
Share on other sites

the Select query worked. I have no idea if the time query worked, because the code died here:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE temphealth < maxhp' at line 1

this is the query:

UPDATE characters SET temphealth = CASE WHEN temphealth + $hp_increase <= maxhp THEN temphealth + $hp_increase ELSE maxhp WHERE temphealth < maxhp

all the colums are right.. I have no idea whats wrong here..

Share this post


Link to post
Share on other sites

the Select query worked. I have no idea if the time query worked, because the code died here:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE temphealth < maxhp' at line 1

this is the query:

UPDATE characters SET temphealth = CASE WHEN temphealth + $hp_increase <= maxhp THEN temphealth + $hp_increase ELSE maxhp WHERE temphealth < maxhp

all the colums are right.. I have no idea whats wrong here..
Still need those query echos.
And check those queries as I explained in phpMyAdmin. It usually gives a little more information about errors.

vujsa

Lets try this:

Change your query to this:
UPDATE characters SET temphealth = CASE WHEN temphealth + $hp_increase <= maxhp THEN temphealth + $hp_increase ELSE maxhp END CASE WHERE temphealth < maxhp

vujsa

Share this post


Link to post
Share on other sites

this is what php my admin says (exactly the same thing?)

SQL query: UPDATE characters SET temphealth = CASE WHEN temphealth + $hp_increase <= maxhp THEN temphealth + $hp_increase ELSE maxhp WHERE temphealth < maxhp MySQL said:  #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE temphealth < maxhp' at line 1

if I am doing it the wrong way, please tell me.
Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

Okay, my fault...

Since I didn't d any queries myself, I forgot a couple of things. ;)

Anyhow, I created a table on my account and used some sample data to run queries against and found the problem. "Quotes"!!!!!

Here is a working query for you:

UPDATE `characters` SET `temphealth` = CASE WHEN `temphealth` + $hp_increase <= `maxhp` THEN `temphealth` + $hp_increase ELSE `maxhp` END WHERE `temphealth` < `maxhp`

As long as you don't have any other problems in the code, this should work now.

vujsa

Share this post


Link to post
Share on other sites

=/ now all the quotes is working fine.. but the value is still not updating =/

here is the code:

<?php$multiplier = 1 / 5;$frequency = 3600 * $multiplier;$increment = 5 * $multiplier;$current_time = time();  // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT hptime FROM characters"), 0);  //  This could be anywhere even in a separate text file!$time_difference = $current_time - $last_update_time;$hours_passed = $time_difference / $frequency; //  convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour$full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour$remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0if($full_hours_passed > 0){	 $new_update_time = $current_time + $remaining_seconds;	 $hp_increase = $full_hours_passed * $increment;mysql_query("UPDATE `characters` SET `temphealth` = CASE WHEN `temphealth` + $hp_increase <= `maxhp` THEN `temphealth` + $hp_increase ELSE `maxhp` END WHERE `temphealth` > `maxhp`");mysql_query("UPDATE characters SET hptime = $new_update_time");}?>

I am not sure whats wrong, but i think it has something with the time to do..
sorry for asking so much vujsa ;) but i don't know how this time() thing works yet..
Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

Well, I figured this might be a problem eventually...

time() gets the unix timestamp from the server. This is the total number of seconds since January 1, 1970. Yeah, it is a very large number.

Anyhow, we work in second because it is the easiest format.

Okay, the script I have given you works globally. This means that the update effects the entire system at once. We aren't accessing each user account individually so the last update value cannot be stored in the user's record. It has to be in its own record preferably in it's own table or in a configuration table.

What I mean is that all users use the same value! So, the field in the 'characters' table named 'hptime' can be deleted!

So, you need a new database table called 'configuration' and it only needs two fields, 'name' and 'value'.
Here is the SQL for the setup:

CREATE TABLE `configuration` (
`name` varchar(32) NOT NULL,
`value` varchar(32) NOT NULL
) ENGINE = MYISAM ;


Just go to your database and run that query to get it setup.

Now, you have to put some value in the table for the script to work:
1202924951
You can get the most recent time here: http://www.unixtimestamp.com/

Remember, the servers are on in the Pacific Time Zone. I believe GMT -8

So that query is just this:

INSERT INTO `configuration` (
`name`,
`value`
)
VALUES (
'last_update',
'1202924951'
);



Once you have your new table set up, then you can adjust you code to use the table like so:


<?php$multiplier = 1 / 5;$frequency = 3600 * $multiplier;$increment = 5 * $multiplier;$current_time = time();  // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT `value` FROM `configuration` WHERE `name` = 'last_update'"), 0);  //  This could be anywhere even in a separate text file!$time_difference = $current_time - $last_update_time;$hours_passed = $time_difference / $frequency; //  convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour$full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour$remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0if($full_hours_passed > 0){	 $new_update_time = $current_time + $remaining_seconds;	 $hp_increase = $full_hours_passed * $increment;mysql_query("UPDATE `characters` SET `temphealth` = CASE WHEN `temphealth` + $hp_increase <= `maxhp` THEN `temphealth` + $hp_increase ELSE `maxhp` END WHERE `temphealth` > `maxhp`");mysql_query("UPDATE `configuration` SET `value` = $new_update_time WHERE `name` = 'last_update'");}?>

Incidently, you could even put your other configuration values in the configuration table like the value for $multiplier.

Okay, I can't think of anything else that might go wrong. By the way, I know that our schedules have been opposite this week but that doesn't really explain why you don't post the information I ask for to figure out why things aren't working... Had you done so, this could have been resolved sooner.

vujsa

Share this post


Link to post
Share on other sites

I'm on my way to give up this script =SIt is still not working.. If you guys can't find a solution for this(because i can't =/) Do you think I should forget this hp regen thing, and use potions instead?@Vujsa: You are right.. I'll do that in the future.

Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

I'm on my way to give up this script =SIt is still not working..
If you guys can't find a solution for this(because i can't =/) Do you think I should forget this hp regen thing, and use potions instead?

@Vujsa: You are right.. I'll do that in the future.

Don't give up, my personal opinion is that if in one way a script don't work i start again and find the solution, that's work.

And try the idea you get, maybe that's the solution.

Best regards,

Share this post


Link to post
Share on other sites

Don't give up, my personal opinion is that if in one way a script don't work i start again and find the solution, that's work.
And try the idea you get, maybe that's the solution.

Best regards,

I'm inclined to agree!

However, you have been shown three ways to accomplish your desired task. At this point in time, you need to choose one and figure it out!

You need to learn what the scripts are actually doing so that you can determine which step isn't getting completed. I have tried my best to explain each bit of code in the script I've supplied you. So, the question is, do you understand the code supplied?

Look up any functions you aren't familiar with and try and figure out the logic. Since this project is as much a learning experience for you as anything, you should keep going.

You need to learn how to perform checks and debug your code...
For example, if you echo every single variable at the end of the script, you should be able to see if there is a problem.

You really need to keep in mind that I haven't run this script! I wrote it as best I could but haven't tested it. So, there very well could be an error in my code, I don't know!

Hope this helps,

vujsa

Share this post


Link to post
Share on other sites

ok.. I know what the error is (the $hp_increase and the $new_update_time have no vaules, but keep reading, Ă want to try to solve it by myself:P). Finally I kinda know how to debug stuff ;)

but i still need i little little help..

 

I need to mess a bit with the "time".

 

Can you please just explain to me, how you created this part(explain it more than what the comments do):

 

$current_time = time();  // get the current server time in second$last_update_time = mysql_result(mysql_query("SELECT `value` FROM `configuration` WHERE `name` = 'last_update'"), 0);  //  This could be anywhere even in a separate text file!$time_difference = $current_time - $last_update_time;$hours_passed = $time_difference / $frequency; //  convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour$full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour$remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0if($full_hours_passed > 0){	 $new_update_time = $current_time + $remaining_seconds;	 $hp_increase = $full_hours_passed * $increment;

because I want to know how I can mess with the time.

I want to know how I can change it to update every minute (just for testing, don't be afraid :P) and how I can change it to update every hour, or everyday and so on.. I want to know how the system works..

When I try to chnage the

$frequency = 60 * $multiplier;
sometimes the hp_increase and new update time becomes 0. sometimes the full_hours_passed becomes many hours.. I know what the errors is, but I don't know how to solve them. The only thing I need to know is what I said earlier. Can you please tell me how to make it update every minute, hour, day or something.

 

Thanks //Feelay

 

Edit:

 

This is how all the Variables looks like:

 

1203074895 current_time1203073225 last_update_time1670 time_difference 2.31944444444 hours_passed2 full_hours_passed230 remaining_seconds1203075125 new_update_time40 hp_increase0.2 multiplier720 frequency20 increment

And theese:

 

-148 time_difference -0.205555555556 hours_passed-1 full_hours_passed-148 remaining_seconds
change all the time. but instead of starting over when they reach 0, they go to the - value, and then, when they reach some value on the "-" they start to become higher (-1, -2, -3, -2, -1, 0, +1, etc.).. maybe I do need a little help here, after all.

 

EDIT2: Ok.. I just found a little small error.. Depending on what value theese things has:

 

90 time_difference 0.125 hours_passed0 full_hours_passed90 remaining_seconds

The value for the $hp_increase and the value for the $new_update_time chnages between (nothing at all, not even 0) and 40 or something (the $new_update_time is much higher ofc...) Now I am really confused.. I don't think I can solve this by myself anymore =/
Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.