php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31960 PATCH: msql_fetch_row() and msql_fetch_array() dropping columns with NULL values
Submitted: 2005-02-13 21:02 UTC Modified: 2005-02-17 21:14 UTC
From: danielc at analysisandsolutions dot com Assigned:
Status: Closed Package: mSQL related
PHP Version: 5CVS-2005-02-13 (dev) OS: NetBSD 2.0
Private report: No CVE-ID: None
 [2005-02-13 21:02 UTC] danielc at analysisandsolutions dot com
Description:
------------
msql_fetch_row() and msql_fetch_array() silently drop columns that contain NULL values.  It's important to retrieve all data from a row, even if it's NULL.  oci8 does this just fine.

While there's a comment in the msql_fetch_array() documentation to watch out for items that have NULL/0/'' values, it doesn't exactly say what the problem is and it only talks about results that have only one column.

Reproduce code:
---------------
$con = msql_connect();
$db  = msql_select_db('ptdb', $con);
msql_query('CREATE TABLE blah (i INT, c CHAR(5))', $con);
msql_query("INSERT INTO blah VALUES (1, 'a')", $con);
msql_query("INSERT INTO blah VALUES (2, NULL)", $con);
msql_query("INSERT INTO blah VALUES (NULL, 'c')", $con);
$result = msql_query('SELECT * FROM blah', $con);
while ($arr = msql_fetch_array($result, MSQL_ASSOC)) {
    var_dump($arr);
    echo "\n";
}
msql_query('DROP TABLE blah', $con);

Expected result:
----------------
array(2) {
  ["i"]=>
  string(1) "1"
  ["c"]=>
  string(1) "a"
}

array(2) {
  ["i"]=>
  string(1) "2"
  ["c"]=>
  NULL
}

array(2) {
  ["a"]=>
  NULL
  ["c"]=>
  string(1) "c"
}

Actual result:
--------------
array(2) {
  ["i"]=>
  string(1) "1"
  ["c"]=>
  string(1) "a"
}

array(1) {
  ["i"]=>
  string(1) "2"
}

array(1) {
  ["c"]=>
  string(1) "c"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-15 02:36 UTC] danielc at analysisandsolutions dot com
I looked at the MySQL extension to see how they handled NULL values and copied that into the mSQL extension.  Here's the patch against head:
http://www.analysisandsolutions.com/php/php_msql.c.bug31960.diff

Works fine on my installation of 5.0.4-dev.
 [2005-02-15 15:43 UTC] danielc at analysisandsolutions dot com
I also put together a patch for the PHP_4_3 branch:
http://www.analysisandsolutions.com/php/php_msql.c.bug31960.4_3.diff
It hasn't been tested, but looking at the source of the MySQL extension makes me think it should work.

Though the script I put in the "Reproduce code" section above doesn't test what happens with empty strings, I subsequently tested the behavior and everything works fine.
 [2005-02-17 21:14 UTC] andi@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Please checkout PHP 4 and PHP 5 CVS trees and make sure it works and compiles (couldn't test).

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 09:01:28 2024 UTC