Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2009
    Posts
    6

    Unanswered: confusing if statements

    I am trying to pull data from a mysql database and have it display different images depending on what the data is. I am very new to php and mysql, so i need some assistance. I found out how to display the data(second have of the code below), but cannot seem to figure out how to work the if statements. I know this is probably something very easy, but once i figure it out, i am sure i can learn to do it on my own. None of teh examples i can find have any explanation of where the items come from, and every tutorial has diff variables, and i can't figure out what matches up to what.

    thanks for any help you can give me


    Code:
    <?
    
    $sql = "select * from birds_sighting";
           $result = mysql_query ($sql);
    
           if ($row="1")
    	   {
    	   $field4= $row["bait_type"];
    	   echo "<img src='bait1.gif'>";
    }
    ?>					
    
    <?
    
    $sql = "select * from birds_sighting";
           $result = mysql_query ($sql);
    
           while ($row = mysql_fetch_array($result))
                  {
                  $field3= $row["bait_amount"];
    
    echo "$field3<br>";
    
                  }
    ?>

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I'm not surprised
    first off I'd layout your code to make it easier on the eye
    PHP Code:
    <?
    $sql 
    "select * from birds_sighting";
    $result mysql_query ($sql);
    if (
    $row="1")
    $field4$row["bait_type"];
       echo 
    "<img src='bait1.gif'>";
    }
    ?>                    

    <?
    $sql 
    "select * from birds_sighting";
    $result mysql_query ($sql);
    while (
    $row mysql_fetch_array($result))
    $field3$row["bait_amount"];
      echo 
    "$field3<br>";
    }
    ?>
    personally I like to layout code as above as I find it easier to idenify missing brackets, however semingly the standard/semi official approach is
    PHP Code:
    if ($row="1") {
      
    $field4$row["bait_type"];
       echo 
    "<img src='bait1.gif'>";

    first off you don't assign a value to the variable $row in the first if block, its as if the if has no relation to the sql statement above.

    you don't need to assign the value from the $row if you are doing no further processing on it

    ie
    PHP Code:
    while ($row mysql_fetch_array($result))
    $field3$row["bait_amount"];
      echo 
    "$field3<br>";

    instead can be rewritten as
    PHP Code:
    while ($row mysql_fetch_array($result))
    {  echo 
    $row["bait_amount"]."<br>";

    incidentally you will help yourself by by giving your script variable more understandable names eg instead of field3 call t, say BaitAmount. the problem is not whilst writing the script, its when you come to revisit the script in 2..3 weeks, months or years
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    In addition to what Mark's said
    Code:
    if ($row="1")
    is just setting the variable $row to the value "1" and not testing that it is "1". You need:
    Code:
    if ( $row == "1" )
    It's also worth using the die statement when running sql queries as they trap errors ie something like:
    Code:
    $result = mysql_query ($sql) or die( "$sql has the error " . mysql_error() );
    Mike

  4. #4
    Join Date
    Oct 2009
    Posts
    6
    Thanks for the help. I will probably get around to changing out the variables once i get the actual code working. however, i am still having a slight problem. When i put the code in as this

    Code:
    <?
    
    $sql = "select * from birds_sighting";
           $result = mysql_query ($sql);
    
           if ($row="1")
    	   {
    	   $field4= $row["bait_type"];
    	   echo "<img src='bait1.gif'><br />";
    }
    it shows the bait1 picture no matter what the value is

    and when i show it as this

    Code:
    <?
    
    $sql = "select * from birds_sighting";
           $result = mysql_query ($sql);
    
    if ( $row == "1" ) {
      $field4= $row["bait_type"];
       echo "<img src='bait1.gif'><br />";
    }  				
    ?>
    it shows the same thing. The bait picture is there even when i change the value to 2 or 3. any ideas on what is wrong?
    Last edited by omegaxero; 10-25-09 at 11:40.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so what is in $row
    where is it set?

    as said before I suspect you want to examine a value based on the SQL. you've execute the SQL, but as far as I can see you are not doing anything with the resultset from that query.

    I suspect you need to assign the result from that SQL using something like while ($row = mysql_fetch_array($result))
    { $field4= $row["bait_type"];
    echo "<img src='bait1.gif'><br />"
    }

    i don't think the
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Oct 2009
    Posts
    6
    What i am trying to do is make it so that when i pull data from the database, it will display it on screen. I can get it to display bait_amount and gold and all those things just fine, but i want it to be able to read the bait_type(1-5 right now) and have it show a picture of a different color bait, so that it will show whichever is equipped.

    I used the code provided with the while part and it works for echoing the numbers, just can't figure out how to make the picture change based on the data it pulls.

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so where is $row set in your sample code
    PHP Code:
    <?

    $sql 
    "select * from birds_sighting";
           
    $result mysql_query ($sql);

           if (
    $row="1")
           {
           
    $field4$row["bait_type"];
           echo 
    "<img src='bait1.gif'>";
    }
    ?>
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Oct 2009
    Posts
    6
    I don't know how to "set" $row. I am very new to php, as i have been doing straight html for a long time. maybe this is something that is to advanced for me.

    I can get the data to show using the "while" command, but i can't figure out how to use the if command. And i cannot seem to figure out how while would change teh picture depending on the variable in the table.

    I want to learn, but i am just not understanding this new coding. Every tutorial i find lists different names for each variable, so i can't even seem to find a correct way to use things, since everyone uses their own specially named variables.

    oh well. thanks for trying to help me out anyway.

Posting Permissions

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