tags:

views:

42

answers:

2

How can I do an if statement based on the value of a mysql table cell. For example, I have a table of people with a column called marital_status. This column has one of two values: yes or no. But this doesn't work:

$query = mysql_query("SELECT marital_status FROM people WHERE first_name = 'John'");

while ($row = mysql_fetch_assoc($query)) {

     $maritalStatus = $row['marital_status'];

}

if ($maritalStatus == "yes") {
   echo "This person is married.";
}
else {
   echo "This person is NOT married.";
}

$maritalStatus == "yes" doesn't return as true even though that is exactly the value in that cell.

+1  A: 

You should move your if block inside the while loop.
And make sure you account for letters case.

$query = mysql_query("SELECT marital_status FROM people WHERE first_name = 'John'");

while ($row = mysql_fetch_assoc($query)) {

     $maritalStatus = strtolower($row['marital_status']);

    if ($maritalStatus == "yes") {
      echo "This person is married.";
    }
    else {
      echo "This person is NOT married.";
    }
}
z-boss
+4  A: 

If you want to work on each line of data returned by your SQL query, you should place your condition inside the while loop -- which iterates over the lines returned by the excution of the query :

$query = mysql_query("SELECT marital_status FROM people WHERE first_name = 'John'");

while ($row = mysql_fetch_assoc($query)) {
    $maritalStatus = $row['marital_status'];
    if ($maritalStatus == "yes") {
       echo "This person is married.";
    }
    else {
       echo "This person is NOT married.";
    }
}


If this doesn't help, it could be useful to check :

  • If the query actually returns results
  • If those results are actually what you think they are.

This can be done using var_dump, to dump the content of a variable. For example, in your situation, you could use :

$query = mysql_query("SELECT marital_status FROM people WHERE first_name = 'John'");
while ($row = mysql_fetch_assoc($query)) {
    var_dump($row);     // Will display the content of $row
}



As a sidenote, you said in a comment that echoing $maritalStatus gets you "Yes".

If there is a Y in uppercase in your database, that's the reason why your code fails : you are testing for "yes" in lowercase.

In PHP, using the == operator for string comparisons, uppercase letters and lowercase letters are considered as different characters.

If you want to compare in a case-insensitive manner, you'll have to either :

Pascal MARTIN
Moving it inside the while loop doesn't help. Var dump reveals this:array(1) { ["marital_status"]=> string(3) "Yes" }
Susan
I see I wrote yes with a lower case y in my original question. But in my actual code the cases are correct. Still no luck.
Susan
@Susan : If you have `"Yes"` with an **uppercase** `Y` in your database, and are testing it against `"yes"` in **lowercase** in your code, that's why your code fails : **uppercase and lowercase letters are not the same character** -- I've edited my answer with some additionnal informations *(much better than a comment ^^ )*
Pascal MARTIN
Oh ;-( ;;; can you edit your question to give us the actual real code you are using ? *(just in case there is another typo somewhere ;-) )*
Pascal MARTIN
I'm so stupid, the problem was in my actual code I didn't put an echo before the "This person is married". Next time I will copy paste my code here instead of writing it from scratch :p
Susan
huhu :-D ;;; yeah, you should always post your real code, as by re-typing it you will most likely not do the same mistakes ;-) ;;; glad you found out what was causing the problem !
Pascal MARTIN