Connecting to Ingres from PHP

This section contains the following topics:

Requirements

Download the PECL Extension

ingres_connect() Function—Connect to Ingres

Code Examples

Previous Topic

Next Topic

Requirements

To connect to Ingres from PHP, you need to download or build the PECL extension and install it. (PECL stands for PHP Extension Community Library, which is a repository for PHP extensions.)

Previous Topic

Next Topic

Download the PECL Extension

Download the source code for the PECL extension for Linux from the following page:

http://pecl.php.net/package/ingres

Or you can build the extension from the latest source code, available from the PHP project's Concurrent Versions System (CVS).

The source code for the Ingres PECL library is stored in the PHP project's CVS server, cvs.php.net. A CVS client is required to access this CVS server. Most Linux distributions have a CVS client available for installation from the installation media an online repository.

Previous Topic

Next Topic

Install the Extension on UNIX or Linux

The installation process for the PECL Ingres library requires a PHP development environment.

Note: You must first obtain the source code from CVS (see Download the PECL Extension) or http://pecl.php.net/get/ingres.

To install the extension on UNIX or Linux

  1. Examine your PATH statement to determine if it contains one of the following commands:
  2. Ensure that the environment variable II_SYSTEM is defined; use the following command to confirm that it is set correctly:

    echo $II_SYSTEM

  3. Move to the directory that contains the source code for the extension.
  4. Generate the configuration file for the extension by issuing the following command:

    phpize

  5. Generate the Makefile needed to build the extension:

    ./configure --with-ingres

  6. Build the extension:

    make

  7. Install the extension:

    make install

    Note: Root access may be required to issue this command.

  8. Edit the php.ini file, adding the following line:

    extension=ingres.so

  9. Ensure that the web server process owner is a valid Ingres user:

    Note: If you use Apache, the Apache directive, User, found in httpd.conf, defines which operating system user runs Apache.

Previous Topic

Next Topic

Configure Apache for UNIX/Linux

If you are using Apache, you must configure it for use with Ingres and the PHP extension. Because the Apache web server does not make available operating system environment variables automatically, additional steps are needed.

Note: The following procedure assumes that Ingres is installed to /opt/Ingres/II.

  1. The shared library mod_env must be loaded to pass variables from the operating system to Apache. Generally, this can be done using the Apache configuration directive:

    LoadModule env_module modules/mod_env.so

    Note: Certain Linux distributions use alternative mechanisms for configuring the modules available to Apache. Check the documentation for your operating system to see what steps are required.

  2. Add the variables II_SYSTEM and LD_LIBRARY_PATH to Apache. Edit the configuration file, httpd.conf, adding the following to the bottom of the file:

    SetEnv II_SYSTEM /opt/Ingres/II

    SetEnv LD_LIBRARY_PATH /opt/Ingres/II/ingres/lib

  3. Restart the Apache server to initiate the changes.

Previous Topic

Next Topic

ingres_connect() Function—Connect to Ingres

You connect to Ingres using the ingres_connect() function and disconnect using ingres_close(). ingres_connect() returns an Ingres II link resource on success, or FALSE on failure.

This function has the following syntax:

resource = ingres_connect([database[,username[,password]]])

Note: If any parameters are missing, ingres_connect() uses the values in php.ini for ingres.default_database, ingres.default_user, and ingres.default_password.

Example: ingres_connect()

<?php

$link = ingres_connect("mydb", "username", "password");

     or die("Could not connect");

echo "Connected successfully";

ingres_close($link);

?>

Note: You can use ingres_pconnect() function to create a persistent connection.

Previous Topic

Next Topic

Code Examples

The following are examples of PHP code you can use for various Ingres operations.

Example: Error checking

<?php

    $link = ingres_connect("mydb", "username", "password");

    if (ingres_errno($link) != 0) {

    echo ingres_errno($link) . " : " . ingres_error($link) . "<BR/>\n";

    }

?>

Example: Simple query

<?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";

    }

?>

Example: Query 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";

    }

?>

Example: 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);

?>


© 2007 Ingres Corporation. All rights reserved.