Connecting to an Ingres database with PHP

December 2016








This tip is based on following Ingres documentation:

Getting started


Ingres® 2006 Release 2 for Windows Quick Start Guide (qs_windows.pdf)
Ingres® 2006 Release 2 for Linux Quick Start Guide (qs_linux.pdf)

Download PECL


Windows (driver) http://pecl4win.php.net/ext.php/php_ingres.dll
Linux & Unix (code source) http://pecl.php.net/package/ingres

Installation


Windows


Copy the file php_ingres.dll in the directory of PHP extensions.
Edit your php.ini and add the following line:
extension = php_ingres.dll 

Linux & Unix


Locate the directory where the command phpize or php-config lies.
On some Linux distributions it is necessary to rename these files to avoid conflicts.
To compile the Ingres PECL library you need a compiler.
Make sure that the variable II_SYSTEM is valued.
echo $II_SYSTEM


Move to the directory containing the source code for the extension :
  • Generate configuration for the extension:


phpize 
  • Build the Makefile:

./configure --with-ingres
  • Compile the extension:


make 
  • Install the extension:


make install


Edit php.ini and add the following line extension = ingres.so
If you use Apache, use the directive users of httpd.conf
to make sure that Apache will run well with a valid Ingres user

Apache for Linux & Unix


In the Apache configuration, add the following lines

LoadModule env_module modules/mod_env.so
SetEnv II_SYSTEM votre-répertoire-II_SYSTEM
SetEnv LD_LIBRARY_PATH votre-répertoire-II_SYSTEM/ingres/lib

Getting connected

  • Getting connected

<?php
$link = ingres_connect(“mydb”, “username”, “password”);
or die(“Could not connect”);
echo “Connected successfully”;
ingres_close($link);
?>
  • Error test

<?php
$link = ingres_connect(“mydb”, “username”, “password”);
if (ingres_errno($link) != 0) {
echo ingres_errno($link) . “ : “ . ingres_error($link) . “<BR/>\n”;
}
?>
  • Simple request

<?php
$link = ingres_connect(“mydb”, “username”, “password”);
// Gives a list of the tables
$sql = “select * from iirelation order by relid asc”;
$rc = ingres_query($sql,$link);
// Do some error checking...
while ( $iirelation = ingres_fetch_object($link) ) {
echo $iirelation->relid “<BR/>\n”;
}
?>
  • Make a request with parameters

<?php
$link = ingres_connect(“iidbdb”, “ingres”, “ingres”);
// Gives a list of the tables based on a parameter
$sql = “select * from iirelation where relowner = ? order by relid asc”;
$params[“owner1”] = (“usrname”);
$rc = ingres_query($sql,$link,$params);
// Do some error checking...
while ( $iirelation=ingres_fetch_object($link) ) {
echo $iirelation->relid “<BR/>\n”;
}
?>
  • Loading a BLOB

<?php
// Fetch the image to be inserted
$handle = fopen (“usrname.png”,”r”);
$login_image = stream_get_contents($handle);
fclose($handle);
// Set up the query
$sql = “insert into login_images values (?,?)”;
// Type the parameters being passed
$types = “vB”; // varchar, BLOB
// Set up the parameter values
$params[“login”] = “usrname”;
$params[“image”] = $login_image;
// Execute
$rc = ingres_query($sql,$link,$params,$types);
?>

Related :

This document entitled « Connecting to an Ingres database with PHP » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.