...

View Full Version : Weird php if condition issues driving me mad



sanomani
07-12-2012, 06:57 AM
With thanks to all codingforum members and volunteers..

I'm facing weird issues for a few days while im working with if condition.
Whenever i use something like


<? if ($x='1') { ...
this always returns $x to be 1, even though my database records say its not.

I am giving a more elaborated example..

My database look like this:
table name: user
username || country
abi || USA
ben || South Africa
chan || Bangladesh

Now when an user logs in, my codes are:

$user = mysql_fetch_object(mysql_query("SELECT FROM `user` WHERE `username` = '$_SESSION['username']'"));

if ($user->country=USA){echo 'proud to be an American';}
else if ($user->country=South Africa){echo 'proud to be a South African';}
else if ($user->country=Bangladesh){echo 'proud to be a Bangladeshi';}


But in my case, all 3 users when they log in sees "proud to be an American"
so the first if condition always becomes true.
This is a simplified form of my codes, but things are like this.


Any suggestion?

Arcticwarrio
07-12-2012, 07:09 AM
You need 2 ==

sanomani
07-12-2012, 07:29 AM
:thumbsup:
You solved this.

And now I am obsessed. I dont know much about coding, I'd tried to code things by taking pieces from internet.
Can you tell me whats whats the difference between single = and double ==
?

djm0219
07-12-2012, 08:54 AM
= is assignment of the value on the right to the variable on the left. == tests for equality which is what you were trying to do.

sanomani
07-12-2012, 09:23 AM
Thanks everybody.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum