supanoob Posted November 28, 2007 Share Posted November 28, 2007 to make it so when an action is performed, the person who did it has to wait 60 seconds before doing it again, without using cronjobs? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 Yes. Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 any idea how? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 http://www.phpfreaks.com/forums/index.php/topic,169496.msg748078.html#msg748078 Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 thanks Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 sorry, i used the link you posted to get this: Â <?php $attack_limit= TIME()-60; $query_1="select * from attack_timers where attacker_id='$account_id' AND attack_time > '$attack_limit'"; $result_1=mysql_query($query_1); if (!$result_1) { die(mysql_error()); } $num_rows=mysql_num_rows($result_1); $row=mysql_fetch_array($result_1); if ($row == '0') { $query="insert into attack_timers (attack_time, attacker_id) values (NOW(), '$account_id')"; $result=mysql_query($query); } else { echo "You must wait 1 minute before attacking another NPC<br><br> <center>[<a href=\"map.php?from=battle\">Back</a>]</center>"; DIE(); } ?> Â but at the moment, even 3-4 minutes after it is still saying you need to wait 1 minute. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 mysql_fetch_array() returns an array. It will never be equal to '0' (unless maybe its empty).  Change this line: if ($row == '0')  to this: echo '<pre style="text-align: left;">' . print_r($row, true) . '</pre>'; if ($row == '0') Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 mysql_fetch_array() returns an array. It will never be equal to '0' (unless maybe its empty).  Change this line: if ($row == '0')  to this: echo '<pre style="text-align: left;">' . print_r($row, true) . '</pre>'; if ($row == '0')  so will it be better to bring up the last line "Account_id" made and use that? if so how would i do that? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 The change I suggested merely prints debugging information. If you copy and paste that information it will help determine what to do next.  Debugging is a simple process that somehow eludes a lot of people. When your program doesn't work, the only way to fix it is to make it give you meaningful information about why it's not working.  You're some of the way there already, as you're printing mysql_error() when things fail. Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 Array ( Â Â [0] => 2 Â Â [attack_id] => 2 Â Â [1] => 19:04:21 Â Â [attack_time] => 19:04:21 Â Â [2] => 1 Â Â [attacker_id] => 1 ) Â thats the information i get when using the debug you gave me (which im greatful for, i have saved it in my notes). Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 That simple debugging statement tells us several things.  First is that up until that point, it appears your code works. (Although you should always confirm that's the record you expected to be pulled from the database).  Second is that right before the if($row == '0') statement, we know that $row is an array. That would explain why your else condition is always triggering.  Looking more carefully at your code, you made a simple logic mistake. In your SELECT statement, you are asking the DB to give you any records from this user within the last 60 seconds. So far so good. You test the result in an if, again so far so good. You then grab the number of rows returned in the result set; again so far so good. Then you use mysql_fetch_assoc() to get the first record in the result set, but why? You don't need it. You've already asked the database for the records within the last 60 seconds and then you asked how many there were, that's all the information you need. If there's zero records, you can process the user's data.  Change: $num_rows=mysql_num_rows($result_1); $row=mysql_fetch_array($result_1); if ($row == '0')  to: $num_rows=mysql_num_rows($result_1); if ($num_rows == 0)  You should also note that in your SELECT query in the beginning, the most records you should ever be able to pull is 1; you can therefore place a LIMIT 1 on the end of the query. On the other hand, you may not want to limit the query because a result set larger than 1 record would be indicative of either a program bug or cheating, and you can have your script notify you (the admin, not your users) if this happens.  Also, just for curiosity's sake, what's the column data type for attack_time? If its DATE, DATETIME, or TIME, you may have a bug on your hands. Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 its currently set at TIME. I will let you know the results after changing what you told me too. Thanks for all your help up too now. Â it is still throwing out that error. I decided to echo out the $attack_limit and this is the result i got along with the error: Â You must wait 1 minute before attacking another NPC. 1196281486 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 28, 2007 Share Posted November 28, 2007 The problem is the column's data type. TIME is a 24 hour format that looks like: HH:MM:SS.  If you look back at the information from the debugging statement earlier, attack_time is equal to 19:04:21.  Using just that and the $attack_limit you echo'ed as example data, your SELECT statement looks like this: select * from attack_timers where attacker_id='$account_id' AND '19:04:21' > '1196281486'  My guess is that MySQL is comparing these values as strings, not as timestamps.  Rather than compute the timestamp in PHP, let's do it in MySQL: select * from attack_timers where attacker_id='$account_id' AND attack_time > (NOW() - INTERVAL 60 SECOND)'  There are two reasons to do this. The first is that it cleans up your PHP; why do work intended for the database in PHP when you can just perform it in the database itself.  The second is more subtle. There is a good chance your MySQL server and apache server are on the same computer, which means time() in PHP and NOW() in MySQL should return the same values. But what happens if the servers are separated to two separate computers, time() in PHP and NOW() in MySQL may no longer return the same value. Uh oh! Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 seems to of worked. Just got to wait for 1 minute now see if i can get off another attack if i can i will post and set as topic solved Quote Link to comment Share on other sites More sharing options...
supanoob Posted November 28, 2007 Author Share Posted November 28, 2007 All working, thanks alot for ya help man. 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.