If statement not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

If statement not working

Post by dreeves »

I am assigning a query to a variable, then I would like to execute that query ONLY if two variables ($a and $b) are equal. both variables are DECIMAL because they are dollar amounts.

I have the following script

Code: Select all

$query6="UPDATE table SET deduction = deduction + $amount, balance = balance - $amount WHERE number=$number";
if ($a == $b)
    {
    mysql_query($query6, $link) or die("query 6 has failed - " . mysql_errno() . ": " .
    mysql_error());
    echo "\$$amount was deducted from the district's balance. <br>\n";
} else {
    echo "The balance for district $number was not changed. <br>\n";
}
I believe there is a problem with my IF statement, because $query6 is executed every single time, even if $a and $b are not equal. Perhaps with the == or quotes. From research I thought it might be because the variables are numbers(DECIMAL). Any guidance is appreciated.
davegmpd
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 9:42 am
Location: London

Re: If statement not working

Post by davegmpd »

Have you tried doing some simple debugging, like adding in:

Code: Select all

 
print_r($a);
print_r($b);
 
This may tell you if/why they're not equal?

Difficult to tell without seeing them being declared.

Dave
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Re: If statement not working

Post by dreeves »

Great idea. I'm new to a lot of this and never think to do any debugging to find out the error.
So, I entered 100 for $a and 90 for $b the result from printing those variables was: 10090
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Re: If statement not working

Post by dreeves »

I just needed single quotes around my variables.

If ('$a' == '$b')
...

Great.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: If statement not working

Post by Mirge »

dreeves wrote:I just needed single quotes around my variables.

If ('$a' == '$b')
...

Great.
uhh................what?
davegmpd
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 9:42 am
Location: London

Re: If statement not working

Post by davegmpd »

Single quotes around your variables will simply make them into strings, not variables.

So '$a' is a string with two characters; $ and a.

I really have no idea why that would work for you.

Why don't you paste more code and people can tell you what's wrong with it.

Dave
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Re: If statement not working

Post by dreeves »

Would using double quotes be a better practice?

Code: Select all

if ("$a" == "$b")
       { mysql_query($query); }
Both variables are dollar amounts stored as type="DECIMAL"
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: If statement not working

Post by jackpf »

No, you dont need quotes.

Try this:

Code: Select all

var_dump($a == $b, $a, $b);
Post Reply