If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > confusing if statements

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-22-09, 21:29
omegaxero omegaxero is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
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>";

              }
?>
Reply With Quote
  #2 (permalink)  
Old 10-23-09, 03:05
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 10-23-09, 03:57
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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
Reply With Quote
  #4 (permalink)  
Old 10-25-09, 10:30
omegaxero omegaxero is offline
Registered User
 
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 10:40.
Reply With Quote
  #5 (permalink)  
Old 10-25-09, 12:50
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 10-26-09, 21:54
omegaxero omegaxero is offline
Registered User
 
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.
Reply With Quote
  #7 (permalink)  
Old 10-27-09, 02:34
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #8 (permalink)  
Old 10-27-09, 16:40
omegaxero omegaxero is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On