Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,694 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,312 people online right now. Registration is fast and FREE... Join Now!




Problem with if statement and 0's

 
Reply to this topicStart new topic

Problem with if statement and 0's, php & mysql

Lilly LaSombra
post 6 Jun, 2008 - 06:29 AM
Post #1


New D.I.C Head

*
Joined: 19 May, 2008
Posts: 4

Hi. I am trying to make an if statement in my php that accesses a variable from my database and echos a sentence if the variable is = 0, but it won't work unless the variable is something other than 0. I've searched but I can't find the workaround besides changing my starting number to 1 instead of 0, but that is not desirable for what i am doing.
here is the code that fails:
CODE
<?php
include ('dbopen.php');
    $result = mysql_query("select * from stats where playernumber=1");
    while($row = mysql_fetch_array($result))
  {
  if ($row['chips']=0)
  {
  echo "My son, I have something for you";
  echo "Here it is";
  }
  else
  echo "It didn't work";
  }
mysql_close($con);
?>


now if I change the value of the chips in the database to 1 instead of 0 and code this it works:
CODE
<?php
include ('dbopen.php');
    $result = mysql_query("select * from stats where playernumber=1");
    while($row = mysql_fetch_array($result))
  {
  if ($row['chips']=1)
  {
  echo "My son, I have something for you";
  echo "Here it is";
  }
  else
  echo "It didn't work";
  }
mysql_close($con);
?>


but I don't want to start with 1. how do i make it work with 0?
I would really like to understand this because it will come up again in my program. Thanks
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 6 Jun, 2008 - 06:49 AM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


php

<?php
include ('dbopen.php');
$result = mysql_query("select * from stats where playernumber=1");
while($row = mysql_fetch_array($result))
{
if ($row['chips']=0) // do you mean this? -> if($row['chips']==0)
{
echo "My son, I have something for you";
echo "Here it is";
}
else
echo "It didn't work";
}
mysql_close($con);
?>


I am not comfortable with PHP [I wrote last line of PHP in Jan 2006 smile.gif]. But All I can find in it was that if statement. see if that comment helps you.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 6 Jun, 2008 - 08:34 AM
Post #3


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(AmitTheInfinity @ 6 Jun, 2008 - 10:49 AM) *

php

<?php
include ('dbopen.php');
$result = mysql_query("select * from stats where playernumber=1");
while($row = mysql_fetch_array($result))
{
if ($row['chips']=0) // do you mean this? -> if($row['chips']==0)
{
echo "My son, I have something for you";
echo "Here it is";
}
else
echo "It didn't work";
}
mysql_close($con);
?>


I am not comfortable with PHP [I wrote last line of PHP in Jan 2006 smile.gif]. But All I can find in it was that if statement. see if that comment helps you.


Amit's solution should work for you. I'm going to rant for quickly on the use of = and ==.

When you said if

CODE

if $row['chips']=0;


What you were really doing was testing to see if $row['chips'] could be set to 0. It then set $row['chips'] to 0. The single equal space is your assignment operator. It assigns the right side to the left variable, but I'm sure you knew that already.

The double sign

CODE

if $row['chips'] == 0;


Tests to see if $row['chips'] is equal to 0. It's very easy to get confused between the two when debugging, as it's something you don't typically look at.

Finaly, there is a triple sign you may see on occasion.

CODE

$x = 5
$y = "5";

if ($x === $y) {
     echo "Same variable type";
}


The triple equals sign checks that the two variables are not only the same value, but the same type of variable. In the above code, $x is an integer with a value of 5, while $y is a string with the character 5. Using $x == $y would result to true, but $x === $y would result to false. PHP is a loosely typed language, as there's no strict use of variable types, so this can get confusing, especially if you've never used another language before.

Hope that's overkill on why the fix is what it is. Take care.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:48AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month