Help, this programming condition always evaluate to true

Discussion in 'Programming' started by Jangga, Jan 12, 2017.

  1. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    149
    Likes Received:
    5
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    $bonusreg = "SELECT * FROM money WHERE username='$user'";
    $bons = mysql_query($bonusreg) or die(mysql_error());
    $dayzzee = $bons["bonus"];

    if($dayzzee==0 && $duration=30){

    My code here

    }

    Dayzzee & duration are numbers from my db. The problem I'm having is this. Please go through them.

    1. If I use if($dayzzee==0 && $duration==30) it works but it always evaluate to TRUE if duration is 30 even when dayzzee is more than 0

    2. If I use if($dayzzee='0' && $duration='30') it doesn't work at all

    3. If I use if(!($dayzzee>0) && $duration=30) it is same as 1 above


    Pls, I'm confused
     
    Last edited: Jan 12, 2017
  2. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    149
    Likes Received:
    5
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    Any suggestion?
     
  3. Fragmaster

    Fragmaster Senior Member

    Joined:
    Apr 3, 2016
    Messages:
    816
    Likes Received:
    1,083
    Gender:
    Male
    what the fuck .
    Do you know difference between = and == ?
     
    • Thanks Thanks x 3
  4. xrvel

    xrvel Junior Member

    Joined:
    Feb 4, 2009
    Messages:
    104
    Likes Received:
    23
    Gender:
    Male
    Home Page:
    I think it should be this

    PHP:
    $bons mysql_query($bonusreg) or die(mysql_error());
    $bons mysql_fetch_assoc($bons);
    $dayzzee $bons["bonus"];
     
    • Thanks Thanks x 1
  5. javabro

    javabro Power Member

    Joined:
    Dec 2, 2015
    Messages:
    720
    Likes Received:
    833
    Gender:
    Male
    umm..
    ever heard of stackoverflow ?
     
    • Thanks Thanks x 1
  6. OutOfContext

    OutOfContext Junior Member

    Joined:
    Oct 31, 2015
    Messages:
    134
    Likes Received:
    25
    Have you tried setting a breakpoint and stepping through your code? That way you can check all variable values on each step.

    If you don't have a debugger set up, you can do var_dump($dayzzee) after it's set to see its value and type.
     
    • Thanks Thanks x 1
  7. ѕмarтgυy

    ѕмarтgυy Newbie

    Joined:
    May 21, 2016
    Messages:
    29
    Likes Received:
    15
    Gender:
    Male
    Occupation:
    Surfer
    Location:
    Antarctica
    Home Page:
    Use triple '=' sign
    PHP:
    if($dayzzee===&& $duration===30)
     
  8. chillerfx

    chillerfx Jr. VIP Jr. VIP

    Joined:
    Mar 20, 2012
    Messages:
    275
    Likes Received:
    82
    Occupation:
    Graphic Designer, Web-dev and Digital Marketer.
    Location:
    Everywhere on net.
    Home Page:
    I think you forget to convert queried results into an array.

    Please use the following line before you assign $bons["bonus"] for $dayzzee.

    $bons = mysql_fetch_assoc($bons);

    Then check your actual condition.

    Before use triple = sign, please confirm the data type what you get from your database.
     
    • Thanks Thanks x 1
  9. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    149
    Likes Received:
    5
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    I got it working. But, I'm curious how do I confirm data type? Sometimes numbers in db appears as string or integer.... I just assume them to always be string but I know it can't always be
     
  10. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    149
    Likes Received:
    5
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    Genius!
     
  11. chillerfx

    chillerfx Jr. VIP Jr. VIP

    Joined:
    Mar 20, 2012
    Messages:
    275
    Likes Received:
    82
    Occupation:
    Graphic Designer, Web-dev and Digital Marketer.
    Location:
    Everywhere on net.
    Home Page:
    You can't get the exact data type from the MySQL query's result. You have to convert your result into datatype, the one which you would like to check with '===' triple equal sign.

    Example:

    Code:
    /* If you would like to check with integer value convert the value into INT */
    $dayzzee = (int)$bons["bonus"];
    if($dayzzee===0 && $duration===30){
     // your code here
    }
    
    /* If you would like to check with Boolean value convert the value into BOOLEAN*/
    $dayzzee = (bool) $bons["bonus"];
    if($dayzzee===true && $duration===false){
     // your code here
    }
    
     
  12. Jangga

    Jangga Junior Member

    Joined:
    Aug 8, 2016
    Messages:
    149
    Likes Received:
    5
    Gender:
    Male
    Occupation:
    Freelancer: A reWriter
    Location:
    Africa
    U are good! I appreciate ur support.