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).

4
  • 1
    when you print_r($row), what do you receive?
    – csi
    Dec 14, 2012 at 16:19
  • Seeing "$row" makes me think you're pulling it from a result set... can you show the code where you get the result set? Are you using mysqli? What you have there should work as long as "$row['rowValue']" has a value.
    – Flat Cat
    Dec 14, 2012 at 16:21
  • Is the column name rowValue ? Dec 14, 2012 at 16:27
  • The dump on the row is ["hasReport"]=> string(1) "0". Which is odd, because the d type in the db is an integer. Dec 14, 2012 at 16:30

5 Answers 5

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.

4
  • The dump on the row is ["hasReport"]=> string(1) "0". Which is odd, because the d type in the db is an integer. Dec 14, 2012 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) as you'll see if you change my code to == 0 it will return true for you in your case.
    – Stu
    Dec 14, 2012 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. Dec 14, 2012 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, 2012 at 17:13
0

What is returning null?

Try this:

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

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

0

maybe try:

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

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

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) { 

https://www.php.net/manual/en/function.array-flip.php

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.