Quick help with IF/ELSE.. PLEASE!

Discussion in 'PHP' started by sitefever, Jun 18, 2007.

Thread Status:
Not open for further replies.
  1. #1
    I know this is really simple but I am not getting the syntax correct.

    I have a page that is already connected to the database. The way this script is written, the contents of a particular field (pd) in the table will always be 0 or 1.

    I am trying to write, if the contents of field "pd" is "1" then "x" else "y"

    Can someone post how I would do that?

    Right now, I have:

    if ( $pd == 1 ) {
    
    //do this
    
    }else{
    
    //do that
    
    }
    Code (markup):
    I think the problem is how I am fetching the data.

    I think that is the part I am screwing up with. Any thoughts please?
     
    sitefever, Jun 18, 2007 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You're not giving us all the details. If you get an error please paste it here. Also, you might want to use _strict_ SQL statements, although I doubt this is the problem:

    
    $SQL = "SELECT * from MANUAL where PD='$pd'";
      - vs -
    $SQL = "SELECT * from `MANUAL` where `PD`='$pd'";
    
    Code (markup):
     
    Evoleto, Jun 18, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Perhaps something like this?
    $sql = "SELECT * FROM `manual`";
    $res = mysql_query($sql);
    
    while ($row = mysql_fetch_assoc($res)) {
        if ($row['pd'] == 1) {
            echo "x"; //do this
        } else {
            echo "y"; //do that
        }
    }
    PHP:
     
    krt, Jun 18, 2007 IP
  4. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    The code you pasted is perfectly valid.
    Make sure you're DB is designed the way your script works, test the following and let us know if you get any error:

    
    $sql = "SELECT * FROM `manual`";
    $res = mysql_query($sql) or die(mysql_error());
    
    while ($row = mysql_fetch_assoc($res)) {
        if ($row['pd'] == 1) {
            echo "x"; //do this
        } else {
            echo "y"; //do that
        }
    }
    
    Code (markup):
     
    Evoleto, Jun 18, 2007 IP
  5. sitefever

    sitefever Banned

    Messages:
    782
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That looks great guys, thanks a lot. I knew it was something simple I was overlooking.
     
    sitefever, Jun 18, 2007 IP
Thread Status:
Not open for further replies.