Bug #1302 Updated: mysql_fetch_row() returns false for row of NULLs
| From: | sander@php.net | Date: | Sun, 16 Jun 2002 12:30:20 +0000 |
| Subject: | Bug #1302 Updated: mysql_fetch_row() returns false for row of NULLs | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-10619@lists.php.net to get a copy of this message | ||
ID: 1302
Updated by: sander@php.net
Reported By: cclarke@netcom.com
-Status: Open
+Status: Bogus
Bug Type: MySQL related
Operating System: Linux
PHP Version: 3.0.7
New Comment:
Thank you for taking the time to report a problem with PHP.
Unfortunately, PHP 3 is no longer supported. Please download
the latest version of PHP 4 from http://www.php.net/downloads.php
If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.
Previous Comments:
------------------------------------------------------------------------
[1999-04-07 12:08:07] cclarke@netcom.com
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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=1302&edit=1