1

Learning PHP and I have a question.

How does one obtain an element from an array and determine if it is equal to a static value? I have a return set from a query statement (confirmed the array has all values).

I tried:

<?  if($row["rowValue"] == 1) { 
    }
?>

I was expecting the value to be 1, but it's returning null (as if I'm doing it wrong).

user82302124
  • 1,143
  • 5
  • 32
  • 57

5 Answers5

0

What is returning null?

Try this:

if($row["rowValue"] === 1) { ... }

Make sure there is an element in $row called rowValue.

Shlomo
  • 3,880
  • 8
  • 50
  • 82
0

maybe try:

<?  if($row[0]["theNameOfAColumn"] == 1) { 
    }
?>

Usually databases return rows like row[0], row[1], row[2], etc.

nathan hayfield
  • 2,627
  • 1
  • 17
  • 28
0

You're pretty much there; something like this should confirm it for you:

echo "<p>Q: Does ".$row["rowValue"]." = 1?</p>";
if($row["rowValue"] == 1) {
    echo "<p>A: Yes ".$row["rowValue"]." does equal 1</p>";
} else {
    echo "<p>A: No, '".$row["rowValue"]."' does not equal 1</p>";
}

If that's still returning 'No' you could try viewing the whole of the $row array by doing a var dump of the array like so:

var_dump($row);

This will give you detailed output of how the array is built and you should be able to see if you are calling the correct element within the array.

Stu
  • 4,160
  • 24
  • 43
  • The dump on the row is ["hasReport"]=> string(1) "0". Which is odd, because the d type in the db is an integer. – user82302124 Dec 14 '12 at 16:29
  • If you pulled this out of a database your array will automatically assume the value is a string, rather than an int. This doesn't matter if you are using a `if($row["rowValue"] == 1)` comparison (it would if you were comparing like so: `if($row["rowValue"] === 1)` [here's a little info on that](http://stackoverflow.com/questions/80646/how-do-the-equality-double-equals-and-identity-triple-equals-comparis)) as you'll see if you change my code to == 0 it will return true for you in your case. – Stu Dec 14 '12 at 16:34
  • So I did a print_r of the variable. It prints 0 when it's zero, and nothing when it's 1. I can't quite understand why. So I changed the comparison to == 0 and it works when it is 0. Any idea why that would be? It's a TINYINT, maxlength of 4. – user82302124 Dec 14 '12 at 16:40
  • it looks like the DB query is returning it as 0, check to make sure the query is returning the results you expect? – Stu Dec 14 '12 at 17:13
0

I am not sure what exactly you are doing, but try using array_filp() which will Exchanges all keys with their associated values than you can do like

if($row["rowValue"] == 1) { 

http://in1.php.net/manual/en/function.array-flip.php

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0

If you're pulling it from mysqli_fetch_row then it wants a number, not a column name. If it's being pulled from mysqli_fetch_array then it will accept a column name.

http://php.net/manual/en/mysqli-result.fetch-row.php

http://php.net/manual/en/mysqli-result.fetch-array.php

Flat Cat
  • 886
  • 4
  • 13
  • 23