Bug #1302: mysql_fetch_row() returns false for row of NULLs
| From: | cclarke at netcom dot com | Date: | Wed, 07 Apr 1999 16:08:08 +0000 |
| Subject: | Bug #1302: mysql_fetch_row() returns false for row of NULLs | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-4974@lists.php.net to get a copy of this message | ||
From: cclarke@netcom.com
Operating system: Linux
PHP version: 3.0.7
PHP Bug Type: MySQL related
Bug description: mysql_fetch_row() returns false for row of NULLs
Note that this may be a superset of bug 1292, though
that description isn't sufficently detailed to be sure.
It seems that database NULL values are not returned in the array returned for a MySQL fetch. So, a
select that returns two values (SELECT A,B FROM TAB) returns an array such that count($array) = 2
when neither A or B is NULL, but if one of A or B is null then count($array) = 1.
The real problem is that when both A and B are NULL for the current row, the return value of
mysql_fetch_row() is logically False (presumably because it's an array with a count() of zero).
This is indistinguishable from False
being returned because you are at the end of the record set.
Example:
MySQL:
CREATE TABLE foo (A INT, B INT);
INSERT INTO foo (A,B) VALUES (1,2);
INSERT INTO foo (A,B) VALUES (NULL,3);
INSERT INTO foo (A,B) VALUES (NULL,NULL);
INSERT INTO foo (A,B) VALUES (4,5);
PHP:
mysql_connect(....
mysql_select_db(...
$cursor = mysql_query("select * from foo");
while($rv = mysql_fetch_row($cursor)) {
$cnt = count($rv);
print "<BR>Count: $cnt<BR>";
for($x = 0; $x < $cnt; ++$x)
print " Value[$x] = " . $rv[$x];
}
Will print:
Count: 2
Value[0] = 1 Value[1] = 2
Count: 1
Value[0] =
Instead of the desired:
Count: 2
Value[0] = 1 Value[1] = 2
Count: 2
Value[0] = Value[1] = 3
Count: 2
Value[0] = Value[1] =
Count: 2
Value[0] = 4 Value[1] = 5
(I am assuming that MySQL will return the rows in the order
they were inserted, which tends to be true. If not the problem will still manifest, but the output
will vary)
What about adding a new special string value like "empty_string" and
"undefined_variable_string" that is "null_string"? Or a flag value that says
an "empty_string" is really a database null?
I could provide a code diff given some idea of how you'd like NULLs to be represented
internally.
Thanks,
-Cam
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net