Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    26
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Weird php if condition issues driving me mad

    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

    Code:
    <? 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:
    Code:
    $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?
    Last edited by sanomani; 07-12-2012 at 06:59 AM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    You need 2 ==

  • Users who have thanked Arcticwarrio for this post:

    sanomani (07-12-2012)

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    26
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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 ==
    ?

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,306
    Thanks
    4
    Thanked 205 Times in 202 Posts
    = 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.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    sanomani (07-12-2012)

  • #5
    New Coder
    Join Date
    Jun 2012
    Posts
    26
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks everybody.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •