php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1302 mysql_fetch_row() returns false for row of NULLs
Submitted: 1999-04-07 12:08 UTC Modified: 2002-06-16 08:30 UTC
From: cclarke at netcom dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 3.0.7 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cclarke at netcom dot com
New email:
PHP Version: OS:

 

 [1999-04-07 12:08 UTC] cclarke at netcom dot 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-16 08:30 UTC] sander@php.net
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.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC