...

View Full Version : something makes a variable the same as another...



MPCODER
04-28-2004, 09:30 AM
Hi,
I've been having probelms with this:
Every time i execute the script it makes a variable the same as another one.
But i can't find what part of the code does it.
Here's my code:

<?php

$db = mysql_connect("localhost", "username", "password");
mysql_select_db("database");
$attackerses = $_SESSION['login'];
$attackersessql = "SELECT user FROM leden WHERE id='$attackerses'";
$attackersessqlresult = mysql_query($attackersessql) or die(mysql_error());
$attackersessqlresultresult = mysql_fetch_object($attackersessqlresult);
$attacker = $attackersessqlresultresult->user;
$attackerhit1 = $_REQUEST["where"];
$attackerhit2 = $_REQUEST["where2"];
$attackerhit3 = $_REQUEST["where3"];
$defenderhit1 = Rand(1, 5);
$defenderhit2 = Rand(1, 5);
$defenderhit3 = Rand(1, 5);
$defender = $_POST['defender'];
$date = date(' YYYY-MM-DD ');
if ($attacker && $defender) {

$powersql = "SELECT won FROM attacks WHERE won='YES' AND user_name='$attacker'";
$powerresult = mysql_query($powersql) or die(mysql_error());
$attackerpower1 = mysql_num_rows($powerresult);

$powersql2 = "SELECT won FROM attacks WHERE won='YES' AND user_name='$defender'";
$powerresult2 = mysql_query($powersql2) or die(mysql_error());
$defenderpower1 = mysql_num_rows($powerresult2);

$poepchinees = $attackerpower1 + 10;
$attackerpower = Rand($attackerpower1, $poepchinees);

$poepchinees2 = $defenderpower1 + 10;
$defenderpower = Rand($defenderpower1, $poepchinees2);

$hityes1 = $attackerpower - 10;
$hityes2 = $attackerpower - 10;
$hityes3 = $attackerpower - 10;

if ($attackerhit1 = $defenderhit1){
$hit = '0';
}
else {
$hit = $hityes1;
}
if ($attackerhit2 = $defenderhit2){
$hit2 = '0';
}
else {
$hit2 = $hityes2;
}
if ($attackerhit3 = $defenderhit3){
$hit3 = '0';
}
else {
$hit3 = $hityes3;
}

$fullattack = $hit + $hit2 + $hit3;

$defense = $defenderpower - 10;

$healthdefender = $defenderpower;
$healthattacker = $attackerpower;

$healthdefenderafterattack = $defenderpower - $fullattack;
$healthattackerafterattack = $attackerpower - $defense;

if ($healthdefenderafterattack > $healthattackerafterattack){
$image = "NO";
}
if ($healthattackerafterattack > $healthdefenderafterattack){
$image = "YES";
}


$existusersql = "SELECT user FROM leden WHERE user='$defender'";
$existuserresult = mysql_query($existusersql) or die(mysql_error());
$existsuser = mysql_fetch_object($existuserresult);

$nope = "0";

if (!$existsuser->user){
echo("Who The Hell is $defender? He isn't registered here.");
}
else {

$attacktimessql = "SELECT * FROM attacks WHERE user_name='$attacker' AND defender='$defender' AND date='$date'";
$attacktimesresult = mysql_query($attacktimessql) or die(mysql_error());
$timesattacking = mysql_num_rows($attacktimesresult);

if ($attackedtoday >= 24){
echo ("You have exceeded your attacks for today");
}
elseif ($timesattacking >= 2){
echo("You can't attack a user more then three times a day");
}
else {

/* Maak SQL-query */
$sql = "INSERT INTO attacks (won, user_name, defender, date)
VALUES ('$image', '$attacker', '$defender', '$date')";
mysql_query($sql);
echo ("You, $attacker have just attacked $defender!<br>$attacker attacks $attackerhit1 !<br> $defender blocks $defenderhit1 !<br><b> Result attack one: $attacker does $hit damage !<b> <br>$attacker shoots $attackerhit2 ! <br> $defender blocks $defenderhit2 ! <br><b> Result Attack 2: $attacker does $hit2 damage!</b> <br> $attacker shoots $attackerhit3 ! $defender blocks $defenderhit3 !<br> <b> Result attack 3: $attacker does $hit3 damage to $defender </b> <br> <b><u>In total, $attacker does $fullattack damage! </b></u><br><i> $defender hits $attacker for $defense damage ! </i><br>Have you wonned the attack: <u> $image </u>! <a href='javascript:history.back()'>Go back</a>");
}
exit;
}
}
?>
it makes $attackerhit1 the same as $defenderhit1.
$attackerhit2 the same as $defenderhit2.
$attackerhit3 the same as $defenderhit3.
Please help me.
Cheers MPCODER :thumbsup:

Mhtml
04-28-2004, 01:48 PM
Which variable?

MPCODER
04-28-2004, 03:14 PM
oh sorry, i thought i included it.
it makes $attackerhit1 the same as $defenderhit1.
$attackerhit2 the same as $defenderhit2.
$attackerhit3 the same as $defenderhit3.
CHeers MPCODER

raf
04-28-2004, 06:54 PM
A clue : if ($attackerhit1 = $defenderhit1){

MPCODER
04-29-2004, 08:08 AM
yes, but should that matter if they are in the if tag?
EDIT: It's solved. I set it up as if ($attackhit1 != $defenderhit1){
$hit = $hityes1; }
else { $hit = '0'; }
And it worked.
CHeers.

raf
04-29-2004, 12:44 PM
I thought it would be ovious, but using "x=y" means "x becomes y" and this doesn't return anything. It just assigns a (new) value to x.
if (x=5){ will just assign the value 5 to x. Which is what you also got because you didn't use a "==" instead of the "="
"x == y" will compaire x with y and if they have the same value, it will return True, if not, it will return False. So this is used inside for insance an if-clause to control the flow of your program --> in most cases, to determine which action needs to be exectuted.
"x != y" does the same, but the returned values are the oposit : returns True is x is defferent from y
"x===y" does the same but also checks if the variable types are equal. (very useful if 0 does not need to be considerat as False, for instance to further proces the returnvalue of strpos() where 0 is not the same a False ).

Mhtml
04-29-2004, 09:12 PM
if(x=y) is sort of irrelevant in PHP, say in C it's a good exception handler for dealing with different data types and not crashing your program.

mordred
04-29-2004, 10:34 PM
I thought it would be ovious, but using "x=y" means "x becomes y" and this doesn't return anything.

No, actually something gets returned. The returned value is the value of the left-hand part of the expression after the operation. In your example it means that "x" is returned, but "x" has the same value as "y" due to the assignment.



if(x=y) is sort of irrelevant in PHP, say in C it's a good exception handler for dealing with different data types and not crashing your program.

Irrelevant? Hardly so. One of the most common idioms to proces the result list of a SQL query went like this (before the "foreach" keyword was introduced, and even nowadays it's been used because foreach creates a copy of the list it iterates on):



while ($row = mysql_fetch_array($result)) {
// do sth. with the dataset...
print $row['number_of_penguins_shot'];
}


This could only work because at the end of the result set, mysql_fetch_array() would return false, thus ending the while loop. And that's just one example. Maybe you meant something different, as I didn't quite understand the part of the C exception handling. Care to elaborate? :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum