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 PECL extension for Windows.

Select the appropriate DLL for the PHP version you are using from the following page:

http://pecl4win.php.net/ext.php/php_ingres.dll

Previous Topic

Next Topic

Download the Windows CVS Client

The PHP project provides anonymous access to the CVS server to enable users to download the code as needed. Windows users can download a compatible CVS client from http://www.cvsnt.org.

To download the Windows CVS client

  1. Log in to the CVS server as follows:

    cvs -d :pserver:[email protected]/repository login

    When prompted for a password, enter phpfi.

  2. Fetch the code for the PECL Ingres library with the following command:

    cvs -d :pserver:[email protected]/repository co pecl/ingres

    This creates two directories, pecl and a subdirectory of ingres. The library code is located in the ingres folder.

Note: If you want to download the source code for PHP, go to http://php.net/anoncvs.php.

Previous Topic

Next Topic

Install the Extension on Windows

After downloading the appropriate extension for your environment, you must install it.

To install the extension on Windows

  1. Copy php_ingres.dll to your extensions directory.
  2. Edit the php.ini file to define the directory that PHP will use, adding the following line for the extension parameter:

    extension=php_ingres.dll

  3. Ensure that the II_SYSTEM directory is defined as a system environment variable:
    1. Click Start, Control Panel, and then double-click the System icon.
    2. The System Properties dialog appears.
    3. Click Environment Variables to view the user and system level environment variables:

      System Properties, Environment Variables

    4. If there is no II_SYSTEM variable defined in the System variables list, click New and enter II_SYSTEM as the new Variable name and the path to Ingres on your system as the Variable value:

      Click OK when you are finished.

  4. Ensure that the web server process owner is a valid Ingres user:
  5. Restart the web server for the changes to take effect
  6. To verify that you have the Ingres extension enabled create the following PHP script in a directory that your web server can access:

    <!-- file : php_setup_info.php -->
    <?php
    phpinfo();
    ?>

  7. Using a web browser, open the script php_setup_info.php. Scroll down the page to locate the section titled "ingres" (similar to the following image, although the extension version and revision number may differ for your installation).

    This will confirm that the extension has been set up and is active. For example:

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.