Reputation: 3472
I have data in my mysql fields which are NULL
But in php when getting the data into an array how come I can compare the value of it without first using php's isset
?
Example,
$var = null;
if ($var == 123123) // GENERATES ERROR AS DIDN'T CHECK IF IT WAS SET FIRST
But in mysql
$q = $dbc -> prepare("SELECT nullColumn FROM table_1");
$q -> execute();
$null = $q -> fetch(PDO::FETCH_ASSOC);
if ($null['nullColumn'] == 2312312) // DOESN'T GENERATE ERROR EVEN THOUGH VALUE IS NULL
I have also used var_dump
on the value and it returns NULL
Upvotes: 2
Views: 1376
Reputation: 522195
$var = null; if ($var == 123123) // GENERATES ERROR AS DIDN'T CHECK IF IT WAS SET FIRST
This premise is false! The above code will not generate an error, because the variable $var
exists.
Trying to use a variable which does not exist, i.e. which hasn't been declared anywhere, generates an error. Trying to use a declared variable whose value is null
is perfectly legit.
See The Definitive Guide To PHP's isset
And empty
for an in-depth look into this topic.
Upvotes: 3
Reputation: 2993
I guess depends on the driver. PDO fetches NULL for NULL fields.
Don't have to use isset, use is_null($field)
or null === $field
Upvotes: 0