Using PHP with Informix
This section demonstrates how to use PHP to access and interact with Informix®.
PHP is a scripting language that can be embedded into HTML documents. When a browser detects the PHP script tag, it hands the script to the PHP processor. HTML is dynamically generated by the PHP script to provide dynamic web page content.
Prerequisites
onstat -
startDemo
/opt/IBM/OpenAdmin/OAT/Apache_version/htdocs
Substitute
the Apache version number that is supplied with Informix Virtual
Appliance for the version in the previously shown
path. The PHP demonstration in the tutorial is called HelloWorld.php and
is already installed in the Apache web server document root directory.
To install a PHP program, copy it to the Apache document root, then
access it through the Firefox browser.Tasks
The HelloWorld.php PHP demonstration accesses the stores database in the demo_on instance.
To run the HelloWorld.php demonstration program, click the following link:
http://localhost/HelloWorld.phpUse a text editor to open HelloWorld.php.
HelloWorld.php is an HTML document that embeds a PHP script that interacts with the Informix server. Everything between <?php and ?> is PHP code.
This example uses the PHP PDO class to establish the connection between PHP and the Informix server. The database connection is opened when the PDO class is instantiated.
<html>
<body>
<?php
try {
$db = new PDO("informix:host=informixva; service=9088;
database=stores; server=demo_on; protocol=onsoctcp;
EnableScrollableCursors=1;", "informix", "informix");
print "Hello World!</br></br>";
print "Connection Established!</br></br>";
$stmt = $db->query("select * from customer");
$res = $stmt->fetch( PDO::FETCH_BOTH );
$rows = $res[0];
echo "Table contents: $rows.</br>";
} catch (PDOException $e) {
print $e->getMessage();
}
?>
</body>
</html>
The statement that begins with $db = new PDO instantiates the PHP PDO module. This statement establishes a connection to the Informix database, as specified by the arguments to the PHP PDO constructor. You can use the using the dbaccess utility and the information in your sqlhosts file to determine database connection information.
cat $INFORMIXSQLHOSTS
The HelloWorld.php example instantiates the PHP PDO module with the argument EnableScrollableCursors=1. This argument is required because the PHP PDO module does not enable scrollable cursors by default.
The last two arguments to the PHP PDO constructor specify the username and password to use when you establish a connection to the Informix server. In this example, the username is informix and the password is informix.
Feel free to modify the program. You can display data or you can create code that interacts with the user and generates dynamic web page content that is based on the user's response.
After you add to or modify the code, test it by copying the file to /opt/IBM/OpenAdmin/OAT/Apache_2.2.16/htdocs. Run the program by opening Firefox and navigating to your new web content.
For example, if the new script is called myTest.php, then navigate to http://localhost/myTest.php.