IF Condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    IF Condition

    Hi,

    I want to display a button that depends on my query result. But my PHP scripts look like didn't understand what I wanted to do (smile).
    Code:
    #check if the file is already exist
          $query3  = "SELECT *   FROM cmr_reviewers 
                            WHERE FileID = {$id}
                            AND reviewer_name = '$user_name'
                           AND reviewed='Y'";
     
          $result3 = mysql_query($query3);
           if($result3==1)
          {
          echo"image 1";
          } 
          else 
           {
          echo "image2";
           }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You're going about your mysql queries all wrong.

    When you do mysql_query($qu ery), that function returns a 'resource id'. You have to then use any of the available functions (mysql_fetch_ar ray, mysql_fetch_ass oc, etc) to use the results of the query.

    So:
    Code:
    while($rows = mysql_fetch_array($result3))
    {
        if($rows['FileID'] == 1)
          {
            echo"image 1";
          } 
        else 
          {
            echo "image2";
          }
    }
    $rows becomes an array of the data returned by the query. You give your column name as the array key.

    Hope this helps.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      A minor nitpick.
      As the mysql_fetch_* functions would only return a single row at a time, naming the variable that holds it's return value $row rather than $rows might be a bit less confusing. (I said minor! :])

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by Atli
        A minor nitpick.
        As the mysql_fetch_* functions would only return a single row at a time, naming the variable that holds it's return value $row rather than $rows might be a bit less confusing. (I said minor! :])
        I'm giving you an infraction for purposely embarrasing me! :P

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by Markus
          I'm giving you an infraction for purposely embarrasing me! :P
          Hehe. Luckily, moderators are immune to infractions! :)
          I had just spent most of the day debugging a Java class, only to find that the entire problem was caused by a single misspelled variable name. So I was a bit touchy about that at the time :P
          (How was I supposed to spot the difference between "fields" and "files" in a single line amongst like 2000 lines!)

          Comment

          Working...