note 105072 added to function.mysql-fetch-array
| From: | final at skilled dot ch | Date: | Tue, 26 Jul 2011 10:50:44 +0000 |
| Subject: | note 105072 added to function.mysql-fetch-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181878@lists.php.net to get a copy of this message | ||
I ran into troubles with MySQL NULL values when I generated dynamic queries and then had to figure
out whether my resultset contained a specific field.
First instict was to use isset() and is_null(), but these function will not behave as you probably
expect.
I ended up using array_key_exists, as it was the only function that could tell me whether the key
actually existed or not.
<?php
$row = mysql_fetch_assoc(mysql_query("SELECT null as a"));
var_dump($row); //array(1) { ["a"]=> NULL }
var_dump(isset($row['a'])); //false
var_dump(isset($row['b'])); //false
var_dump(is_null($row['a'])); //true
var_dump(is_null($row['b'])); //true + throws undefined index notice
var_dump(array_key_exists('a', $row)); // true
var_dump(array_key_exists('b', $row)); // false
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.219.216.93
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php
Edit -- https://master.php.net/note/edit/105072
Del: integrated -- https://master.php.net/note/delete/105072/integrated
Del: useless -- https://master.php.net/note/delete/105072/useless
Del: bad code -- https://master.php.net/note/delete/105072/bad+code
Del: spam -- https://master.php.net/note/delete/105072/spam
Del: non-english -- https://master.php.net/note/delete/105072/non-english
Del: in docs -- https://master.php.net/note/delete/105072/in+docs
Del: other reasons-- https://master.php.net/note/delete/105072
Reject -- https://master.php.net/note/reject/105072
Search -- https://master.php.net/manage/user-notes.php