PDO_IBM Hiccup
Last Post 29 Mar 2013 10:50 AM by Alex Krashevsky. 2 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Alex Krashevsky
New Member
New Member
Posts:27

--
04 Feb 2013 08:55 PM

So I have developed a light PHP script (actually, an exercise from J.Olen/K.Scroeder PHP book). It creates a PDO object connecting to DB2 database on the i, prepares and run an SQL statement and displays the result set in an HTML table. The following coding block goes over the result set.

while (($row = $stmt->fetch((PDO::FETCH_OBJ))) !==false) {
    echo ''.$row->FIRSTNAME.'';
    echo ''.$row->LASTNAME.'';

Here FIRSTNAME and LASTNAME are actual field (column) names from the query. It's all well and good; however, the script produces a blank HTML on me. Running this script over Zend Debugger, I've noticed the following in the console.

Undefined property: stdClass::$FIRSTNAME

Undefined property: stdClass::$LASTNAME

At the same time, I could see the actual string variables for $row->FIRSTNAME and $row->LASTNAME in the Variables pane. 

$row    Object of: stdClass    
    FIRSTNAME    (string:6) Mickey    
    LASTNAME    (string:5) Mouse  

 
If I use FETCH_ASSOC instead of FETCH_OBJ, it works perfectly.

while (($row = $stmt->fetch((PDO::FETCH_ASSOC))) !==false) {
    echo ''.$row['FIRSTNAME'].'';
    echo ''.$row['LASTNAME'].'';

Also, the original script (with FETCH_OBJ) works just fine if slightly modified for use with an identical MySQL table (and MySQL PDO driver).

So, what might be wrong with my original script?

 

 

 

John Rahn
New Member
New Member
Posts:49

--
06 Mar 2013 04:19 PM
FETCH_OBJ will return an anonymous object with the column name as the property. Are you sure that's what you're looking to achieve? If you're looking to get data, FETCH_ASSOC or FETCH_BOTH is your best bet.

Here's the page from the manual:

- http://www.php.net/manual/en/pdosta....fetch.php

HTH
Alex Krashevsky
New Member
New Member
Posts:27

--
29 Mar 2013 10:50 AM
Well, I was just following along with the Jeff's and Kevin's IBM i PHP book. Both PDO methods are listed there as legitimate data retrieval means. Once again, both methods worked OK in my test for MySQL. There must be something DB2 specific there. I am not trying to say that the issue is of huge significance to me since there is obviously workaround, was just wondering if there is someone in the community with successful experience utilizing PDO FETCH_OBJ on the i.

Besides the authors ;-)
You are not authorized to post a reply.

Acceptable Use Policy