WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Simple Connection to Informix with PHP
Categories : PHP, Informix, Databases Picture not available
Meloni Julie
Date : 2000-01-16
Grade : 0 of 5 (graded 0 times)
Viewed : 18920
Search : More Articles by Meloni Julie
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


While not the most frequently-used database type by PHP programmers, there must be one or two out there who use Informix, else the built-in functions wouldn't exist. While there are numerous fuctions for PHP -> Informix connectivity,
documented in detail in the PHP Manual, you only need a few of these functions in order to make a simple connection and select some data:



ifx_connect - opens a connection to the Informix server; requires a database name, username and password.



ifx_query - issues the SQL statement.



ifx_htmltbl_result - places the results of your SQL statement into a nicely-formatted HTML table.





ifx_free_result - frees the resources in use by the current connection.



ifx_close - closes the current connection.



Just for argument's sake, let's pretend that Informix is already installed on your system, and you have a valid username and password for an existing database. Let's also assume that you've created a table on that database, called COFFEE_INVENTORY. The COFFEE_INVENTORY table has three columns: COFFEE_NAME, ROAST_TYPE and QUANTITY.



The rows in the COFFEE_INVENTORY table could be populated with data such as:



French Roast,dark,18



Kenya,medium,6



Ethiopian Harrar,medium,35



Sumatra,dark,8



Columbian,light,12



Now, let's do some PHP. Before you begin, you must know the name of the server on which the database resides, and have a valid username and password for that server. Then, start your PHP code by creating a connection variable:


<?
$connection
= ifx_connect("dbname@SERVERNAME", "username", "password");


Test that a connection was established and if it wasn't, print an error message and exit the program:


if (!$connection) {
echo "Couldn't make a connection!";
exit;
}


If you've made it this far, you can issue a SQL statement and hopefully see some results! Using the COFFEE_INVENTORY table, suppose you want to view your inventory, including the name of the coffee and the roast type, with the highest number of bags listed first. Create a variable that holds your SQL statement:



$sql = "SELECT COFFEE_NAME, ROAST_TYPE, QUANTITY
FROM COFFEE_INVENTORY
ORDER BY QUANTITY DESC";





Next, create a variable to hold the result ID of the query, carried out by the ifx_query function. The ifx_query function takes two arguments: the connection and SQL statement variables you've just created.


$sql_result = ifx_query($sql,$connection);



To format the results currently held in $sql_result, use the very handy ifx_htmltbl_result function. This function uses the result ID variable and HTML table options, such as "border=1".


ifx_htmltbl_result($sql_result,"border=1");



Finally, you'll want to free up the resources used to perform the query, and close the database connection. Failing to do so could cause memory leaks and other nasty resource-hogging things to occur.


ifx_free_result($sql_result);
ifx_close($connection);

?>


The full script to perform a simple connection and data selection from an Informix database could look something like this:


<?php

// create connection
$connection = ifx_connect("dbname@SERVERNAME", "username", "password");

// test connection
if (!$connection) {
echo
"Couldn't make a connection!";
exit;
}

// create SQL statement
$sql = "SELECT COFFEE_NAME, ROAST_TYPE, QUANTITY
FROM COFFEE_INVENTORY
ORDER BY QUANTITY DESC"
;

// execute SQL query and get result
$sql_result = ifx_query($sql,$connection);

// format result in HTML table
ifx_htmltbl_result($sql_result,"border=1");

// free resources and close connection
ifx_free_result($sql_result);
ifx_close($connection);

?>


Please see the PHP Manual for additional Informix database functions, and try using your own tables and SQL statements instead of the examples above.









User identification using cookies in PHP and MySQL
Categories : PHP, Databases, MySQL, Cookies
Simple Connection to Microsoft SQL Server with PHP
Categories : PHP, MSSql, Databases
Creating Auto-incrementing ID Fields with PHP and Oracle
Categories : PHP, PHP options/info, Databases, Oracle
Simple Connection to mSQL with PHP
Categories : PHP, mSQL, Databases
Custom MySQL-functions
Categories : Databases, MySQL, PHP, PHP Functions
Database Abstraction with PEAR
Categories : PHP, Pear, Databases
Watching The Web
Categories : PHP, Databases, MySQL, HTTP, MD5
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
Simple Connection to Oracle with PHP
Categories : PHP, Oracle, Databases
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
Introduction to using PHP 5.0 and SQLite
Categories : PHP, SQLite, Databases
Simple Connection to PostgreSQL with PHP
Categories : PHP, PostgreSQL, Databases
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Referer Statistics
Categories : PHP, MySQL, HTTP, Databases