0

I'm successfully connecting to the database, and successfully pulling data and displaying it using db2_fetch_array and db2_fetch_both. The code below works just fine

$file="m03slsd0";
$file=db2_escape_string($file);
$query="SELECT slgrpn,slfrkn,slftyp,slfsze,slpqty,slpwht,slentp,slkplt FROM HUTALIB.$file";
$quepre=db2_prepare($conn,$query);
$quexe=db2_execute($quepre);

while($row=db2_fetch_both($quepre))
{
$det=$row[0];
if($det!='')
{
printf($det."</br>");
}
}

The problem comes up when i'm changing the index to column name in db2_fetch_assoc() or db2_fetch_array() - the code below prints nothing.

while($row=db2_fetch_both($quepre))
{
$det=$row['slgrpn'];
if($det!='')
{
printf($det."</br>");
}
}

Any suggestions?

Thanks in advance

6
  • This calls for basic debugging. What do the results contain? Do you get a database error? Is error reporting activated? Does $row['slgrpn'] actually contain a value? Commented Oct 5, 2011 at 19:50
  • $row['slgrpn'] should contain the value $row[0] contains. I don't recieve any error, furthermore, the only problem is there's not output while there shoold be output becuase as i said before, $row[0] contains a value which is successfully printed through the first while. Commented Oct 5, 2011 at 19:53
  • Well, does it contain anything? Because if it doesn't, nothing will be output. Commented Oct 5, 2011 at 19:54
  • This one works just fine : while($row=db2_fetch_both($quepre)) { $det=$row[0]; if($det!='') { printf($det."</br>"); } } Altough this one prints nothing, while it should print exactly what the previous while loop printed while($row=db2_fetch_both($quepre)) { $det=$row['slgrpn']; if($det!='') { printf($det."</br>"); } } Commented Oct 5, 2011 at 19:57
  • 1
    Ah, I see now. That's different, sorry. Still, test outputs will help. What does a print_r($row) show? Commented Oct 5, 2011 at 19:58

2 Answers 2

1

Array ( [SLGRPN] => 12626 ...

Array keys are case sensitive you you will need to use

$det=$row['SLGRPN'];

not sure why the field names get turned into uppercase - it might be a characteristic of db2.

Sign up to request clarification or add additional context in comments.

1 Comment

It is indeed a characteristic of DB2.
1

DB2 identifiers are case-insensitive by default, and will use/return uppercase field names, unless your columns are defined within double quotes (same applies for table names):

CREATE TABLE foo ( bar integer, "baz" integer );

Querying this table:

SELECT bar, "baz" FROM foo;

... in PHP would return something like:

Array (
    [BAR] => something
    [baz] => something   
)

So you'd have to do:

echo $array['BAR'];
echo $array['baz'];

To remove any ambiguity, you can change your queries:

SELECT BAR FROM FOO;

...or define all fields and table names with double quotes:

SELECT "bar", "baz" FROM "foo"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.