Jump to content

[SOLVED] is it possible


supanoob

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

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.