Undefined index when using Zero

I am trying to get information from a database row/column. One of the fields contains the integer 0. Alternatively, it can contain 1. (0 for false, 1 for true). When I try to check this in php using the usual $row[‘value’] I receive an undefined index notice. I have checked and I am definitely getting the value of 0 - even php reports it as a 0 in a var dump. What is going on here and what can I do about it? It will make my code a lot less legible to use an ISSET to check for the value just to make sure the db result is not a 0, when I would rather check for the correct db result directly with if ($row[‘value’] == true) { }. I am also worried about future, unexpected, complications when the db might return 0 as the legitimate query result but php won’t recognize the result as being defined.

Can you post some code? I’m pretty sure you are checking it incorrectly, need to see what’s going on.

try using === instead of ==

Please post your current SQL statement.

Here you go:

(The class this is in extends a pdo object, so that’s why I am calling this-> to run db queries.)

	$sql = "SELECT id_user, account_status FROM users WHERE user_email = :user_email";
	$stmt = $this->prepare($sql);
	$stmt->bindValue(':user_email', $email);
	$row = $stmt->fetch();
catch (PDOException $e)
	// error handling

// var_dump($row['account_status']) // gives int 0 for this result, showing it is set correctly

if ($row['account_status'] == true)
	// do true stuff
	// do not true stuff

The first thing I tried was using === instead of ==, no luck though. :frowning:

$row[‘account_status’] == ‘1’

Try this:

echo '<pre>';
var_dump( $row );
echo '</pre>';

Tapped from a tablet

Thanks for your help, arout and John. I think I found the problem (which I will attempt to fix tomorrow; it’s a little deeper than the code snippet above, I think). It was just user error. While php was returning the value, it was nested one level deeper in the array, and I overlooked this because there were not multiple arrays returned so it didn’t stand out to me. At a brief glance I only saw that the result was returned with the value I needed, so I looked up and down elsewhere to find out the problem.

'Glad you managed to solve the problem, usually best to start at the beginning and work towards the problem.

Hope you have a good weekend :slight_smile:

Thanks, and just to report, it was the nested array. It wasn’t until I posted this I made the breakthrough (which I should have seen immediately!).