Thursday, May 15, 2008

Informix meets Wampserver 2 Part 2

The most beautiful string in a PHP-Informix programmer's life:


$db = new PDO("informix:host=;service=;database=;
server=;protocol=onsoctcp;DB_LOCALE=en_US.8859-1", "username", "password");


PDO constructor: PDO("db:xxxx", "username", "password");

host_computername: the name of the server hosting informix, if unsure, check /etc/hosts

service_port: the port number used by the server, if unsure, check $INFORMIXDIR/etc/sqlhosts for the service name then go to /etc/services to find out the port number

db_name: name of the database you want to connect to, obviously...

server_name: name of the server, don't get confused with host. Yeah, you're already confused...

protocol: I really got no idea what this is, and what difference it makes, will post later when I find out more.

DB_LOCALE: Sets the locality, but I think its more useful in killing this error


'PDOException' with message 'SQLSTATE=HY000, SQLDriverConnect: -23197 [Informix][Informix ODBC Driver][Informix]Unspecified System Error = -23197.'


The username and password is the one you use to log in the linux host, so username must be informix.

I also adapted code copied from http://www.informix-zone.com/informix-script-dbapi#php
to test out the connection string.

<?php
# ------------------------------------
# open connection to database 'stores'
# ------------------------------------
try {
$dbh = new PDO("informix:host=example.com;service=1234;database=stores;
server=myserver;protocol=onsoctcp;DB_LOCALE=en_US.8859-1", "informix", "123456");
}
catch (exception $e) {
print $e;
}
# ----------------
# open transaction
# ----------------
$dbh->beginTransaction();

# --------------------------------------
# prepare and execute 'select' statement
# --------------------------------------
$sth1 = $dbh->prepare("select bank_code, bank_name from bank");
$sth1->execute();
# ---------------------
# fetch thru result set
# ---------------------
while( $row = $sth1->fetch() )
{
# --------
# show row
# --------
//echo "<pre>";
//print_r($row);
//echo "</pre>";
echo $row[BANK_CODE] . ' - ' . $row[BANK_NAME] . '<br/>';
}
$dbh = null;

?>

No comments: