|
This section contains the following topics: |
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.)
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.
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
Note: Certain distributions of Linux rename these files to avoid version conflicts. To build the PECL Ingres library you need a C compiler.
echo $II_SYSTEM
phpize
./configure --with-ingres
make
make install
Note: Root access may be required to issue this command.
extension=ingres.so
Note: If you use Apache, the Apache directive, User, found in httpd.conf, defines which operating system user runs Apache.
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.
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.
SetEnv II_SYSTEM /opt/Ingres/II
SetEnv LD_LIBRARY_PATH /opt/Ingres/II/ingres/lib
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]]])
Specifies a database with the following syntax:
[vnode_id::]dbname[/svr_class]
Specifies the virtual node name used to connect to a remote machine
Specifies the database name
Specifies the Ingres server class, which defaults to INGRES if not specified. It is used when connecting to different server classes.
Specifies an Ingres user name to use for the connection
Specifies the password for the user name
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.
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);
?>