Using PDO for database access

General discussion on Zend Server for IBM System i

Using PDO for database access

Postby eaburk on Tue Nov 06, 2012 7:46 pm

I'm interested in using PDO for accessing to db2. I've searched the internet and haven't been able to put the pieces together to tell me a) if it's possible b) if it's practical and c) how to setup/use it. Does anyone here us it? If so what did you do to get it to work - code samples are welcome!
eaburk
 
Posts: 74
Joined: Mon Jan 04, 2010 3:42 pm

Re: Using PDO for database access

Postby raytaylorii on Wed Nov 14, 2012 6:21 pm

I have used PDO to access the IBM i. In most cases, it works well. The one problem I found was when I call a stored procedure and need to pass numeric data, the data is not translated correctly and the receiving IBM i program gets garbage data. Here are a couple of examples of using it to execute sql statements:

Code: Select all
//------------------------------------------------------------------
// Get connection to a database.  This will only return a new connection
// if there is not one already declared.  The static declaration of
// $dbConnection keeps it's value  so if it has already been created,
// the existing connection is returned.
function getDBConnection() {
    static $dbConnection;
    if (is_null($dbConnection) || !isset($dbConnection)) {
        try {
            $dbConnection = new PDO('ibm:*LOCAL',
                            UserName, UserPw);
            $dbConnection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);   
        } catch (PDOException $e) {
            trigger_error("Error: " . $e->getMessage());
        }
    }
    return $dbConnection;
}

// Executing sql statement:
   $sql = 'select count(*) as invCount from shlib.srdsum where oscomp = ? and
      osinvc = ? and oscust = ?';
   try {
      $dbConn = getDBConnection();
      $pStmt = $dbConn->prepare($sql);
      $pStmt->execute(array($Company, $Invoice, $Customer10));
      $row = $pStmt->fetch();
      if ($row['INVCOUNT'] == 0)
         $okToShow = false;

   }
   catch (PDOException $e) {
   
//---


Hope this helps....
raytaylorii
 
Posts: 27
Joined: Tue Sep 29, 2009 4:35 pm


Return to Zend Server for IBM i

Who is online

Users browsing this forum: No registered users and 1 guest