php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6627 pg_fetch_array omits field names in case of NULL values
Submitted: 2000-09-08 09:11 UTC Modified: 2000-09-10 10:06 UTC
From: c_froehler at lycosmail dot com Assigned:
Status: Closed Package: PostgreSQL related
PHP Version: 4.0.2 OS: Linux 2.2.12
Private report: No CVE-ID: None
 [2000-09-08 09:11 UTC] c_froehler at lycosmail dot com
After upgrading from PHP 4.0.1pl2 to 4.0.2 pg_fetch_array acts differently if the optional third parameter is missing or is set as PGSQL_BOTH 

Prior to the upgrade,  it created the associative values (with field names) as well as values that could be accessed by index numbers for all values.

But with 4.0.2, for fields that have a NULL value, the associative array entry is no longer generated. This results in a PHP warning message when the entry is to be read (and breaks my application).

This was reproducable on two machines, both PHP4.0.2, PostgreSQL 7.0.0, Linux 2.2.12

Example:
in PostgreSQL:
CREATE TABLE tblTest (fldInteger INT4, fldVarchar VARCHAR(20));
INSERT INTO tblTest (fldInteger, fldVarchar) VALUES (123, 'PHP rocks');
INSERT INTO tblTest (fldInteger, fldVarchar) VALUES (null, null);

PHP Script
<?php

// query example data
$db=pg_connect("dbname=mydb port=5432 user=pguser password=");
$result = pg_exec ( $db, "SELECT fldInteger, fldVarchar FROM tblTest;");

// loop through result set and show each field name and content
for ( $i=0; $i < pg_numrows($result) and 
	$resultrow = pg_fetch_array($result, $i, PGSQL_BOTH); $i++)
{
	echo "Record #".$i."<br>";
	foreach($resultrow as $key=>$val)
	{ echo $key."=>".$val."<br>"; }
	echo "<br>";
}

?>

The output of this script is the following, if the third param of pg_fetch_array is set to PGSQL_BOTH or omitted:
Record #0
0=>123
fldinteger=>123
1=>PHP rocks
fldvarchar=>PHP rocks

Record #1
0=>
1=>

If the third param is PGSQL_ASSOC, this is the output:
Record #0
fldinteger=>123
fldvarchar=>PHP rocks

Record #1
fldinteger=>
fldvarchar=>

And here is my conficure line:
"./configure" \
"--with-pgsql" \
"--enable-trans-sid" \
"--disable-debug" \
"--enable-ftp" \
"--with-apxs=/www/bin/apxs" \
"--enable-track-vars" \
"$@"                         

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-10 10:06 UTC] jah@php.net
Fixed in current CVS. Fetch the latest version of ext/pgsql/pgsql.c from
http://cvs.php.net/viewcvs.cgi/~checkout~/php4/ext/pgsql/pgsql.c?rev=1.68
and recompile.

As as side note, this mistake happened when fixing/changing the returned value of
NULL columns. In your example, you could now test if ($val === NULL).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 16:01:29 2024 UTC