Close menu

Using PHP Data Objects (PDO)

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.

The SQL/R A.03 or newer ODBC driver is compatible with PHP PDO. Please note that the ODBC cursor library needs to be disabled by setting the USE_CURSOR_LIBRARY attribute to USE_DRIVER in the new PDO() constructor call, as shown in the example below.

This is an example HTML document showing the use of PHP PDO to connect to a data source specifying the Eloquence example database, then preparing a SELECT statement and executing it with two parameters. The example also demonstrates how the iconv() function may be used to convert the result from ISO-8859-1 to UTF-8:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Test: PDO Prepare/Execute</title>
</head>
<body>
<?php

   try {
      $db = new PDO('odbc:sample', 'public', '', array(
         PDO::ODBC_ATTR_USE_CURSOR_LIBRARY => PDO::ODBC_SQL_USE_DRIVER
      ));

      $st = $db->prepare(
         'SELECT custno, name1, name2 FROM customers' .
         ' WHERE custno BETWEEN ? AND ?'
      );

      $st->execute([ '13000', '13999' ]);

      while ($row = $st->fetch(PDO::FETCH_ASSOC))
         echo iconv('ISO-8859-1', 'UTF-8', print_r($row, true)) . "<br/>\n";
   }
   catch (PDOException $e) {
      echo $e->getMessage();
   }

?>
</body>
</html>