entire Powerpoint presentation
Download
Report
Transcript entire Powerpoint presentation
Beginners Guide to
Web-Enabling your
Informix database
Using:
By:
Peter Schmidt
PRS Technologies, Inc.
What is PHP ?
PHP (officially "PHP: Hypertext Preprocessor")
• ( Originally “Personal Home Page” )
• A server-side HTML-embedded scripting language.
• Writing a database-enabled web page is incredibly simple !
• PHP is in use on over 150,000 sites around the world.
• Since PHP is executed on the web server, web browsers
can’t see your PHP code.
• PHP is very fast.
• PHP is completely free (Open Source)
PRS
Technologies, Inc.
October, 2000
2
The following databases are
currently supported:
• Informix
InterBase
PostgreSQL
• dBase
FrontBase
Solid
• Empress
mSQL
Sybase
• FilePro (read-only)
Direct MS-SQL
Unix dbm
• IBM DB2
MySQL
Adabas D
• ODBC
Velocis
Ingres
• Oracle (OCI7 and OCI8)
PRS
Technologies, Inc.
October, 2000
3
An introductory example of PHP
<html>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
Note that PHP code is embedded inside the HTML code.
PRS
Technologies, Inc.
October, 2000
Demo 1
4
Switching between HTML and PHP
<html>
<body>
HTML
Here I am in HTML! <BR>
Note the
<?php
PHP start
echo ”Now I'm in PHP! <BR>";
and stop
?>
tags
PHP
Back to HTML! <BR>
Back to
HTML
</body>
</html>
PHP code is executed on the web server!
PRS
Technologies, Inc.
October, 2000
Demo 2
5
Putting comments in PHP code
<?php
echo "This is a test <br>";
// This is a one-line c++ style comment
echo "This is a test <br>";
# This is Unix shell-style style comment
/* This is the first line of a multi line comment
This is the second line of comment */
?>
PHP supports 'C', 'C++' and Unix shell-style comments.
PRS
Technologies, Inc.
October, 2000
6
Connecting to the Informix database
<?php
$database
$server
$login
$password
= "video";
= "lapdog_tcp";
= ”username";
= ”password";
$dbs = $database . "@" . $server;
/* populate variables */
/* concatenate */
$connect_id = ifx_pconnect($dbs,$g_login,$g_password);
if (!$connect_id) {
echo "Unable to connect to Informix database<br>\n";
chk_ifx_err1($connect_id);
This is my user
} else {
echo “Informix connection successful! <br>”; defined function
}
?>
PRS
Technologies, Inc.
October, 2000
Demo 3
7
Use a tcp/ip connection to avoid error -27000
<?php
$database
$server
$login
$password
= "video";
= "lapdog_tcp";
= ”username";
= ”password";
Use a tcp/ip connection to the
database
(vs. a shared memory connection)
?>
Warning: E [SQLSTATE=IX 000 SQLCODE=-27000] in
/u/www/lapdog/php_demo/php_demo4.php3 on line 40
Unable to connect to Informix database
Informix error: Cannot support multiple connections over shared memory.
finderr -27000
-27000 Cannot support multiple connections over shared memory.
An application cannot use the CONNECT statement to make more than one connection that uses shared-memory
communication (IPC).
Ensure that the application makes only one shared-memory connection at a time. If the application must use concurrent
connections, the database server administrator might need to change the connection type
(as specified in the nettype field of the sqlhosts file) from a shared -memory connection to a network connection.
PRS
Technologies, Inc.
October, 2000
Demo 3
8
Persistent database connections
• An SQL connection that does not close when the
execution of your script ends. $connect_id = ifx_pconnect();
• When a persistent connection is requested, PHP checks
if there's already an identical persistent connection (that
remained open from earlier) - and if it exists, it uses it.
• If it does not exist, it creates the connection.
• if connection overhead is high, persistent connections
help you considerably.
• It may (and probably will) change the efficiency of the
script, but should not change its behavior!
PRS
Technologies, Inc.
October, 2000
9
Reporting an Informix error
<?php
User defined function
function chk_ifx_err1($result_id) {
$err_string = ifx_error($result_id);
Get the error string
/* Note: error If 1st character of err_string is not blank */
if ($err_string[0] != ' ') {
Check for blank
printf("Informix error: %s", ifx_errormsg());
die;
End program
}
}
?>
Warning: E [SQLSTATE=IX 000 SQLCODE=-952]
in /u/www/lapdog/php_demo/php_demo4.php3 on line 40
Unable to connect to Informix database
Informix error: User's password is not correct for the database server.
PRS
Technologies, Inc.
October, 2000
Demo 4
10
Selecting a count from a table
<?php
if ($connect_id) {
$statmt_txt = "select count(*) count from vhs where type_code = 'ST1' ";
$statmt_id1 = ifx_query($statmt_txt,$connect_id);
// EXECUTE SQL
if (!$statmt_id1) { chk_ifx_err1($statmt_id1); }
// CHECK FOR ERROR
$row = ifx_fetch_row($statmt_id1);
// FETCH RESULT OF COUNT INTO ARRAY
if (!$row) { chk_ifx_err1($statmt_id1); }
// CHECK FOR ERROR
$num_rows_selected = sprintf("%d",$row[count]);
echo "<html>";
echo "$num_rows_selected rows selected <BR>";
echo "</html>";
// FORMAT COUNT
// DISPLAY RESULT IN HTML
}
?>
PRS
Technologies, Inc.
October, 2000
Demo 5
11
Selecting rows
Output to a HTML table.
Use display label for clarity
<?php
$statmt_txt = "select tape_num Tape_Number ,title from vhs where type_code = 'ST1' order by title";
$statmt_id = ifx_prepare($statmt_txt,$connect_id,IFX_SCROLL);
if (!$statmt_id) { chk_ifx_err1($statmt_id); }
$ret_val = ifx_do($statmt_id);
// PREPARE
// CHECK FOR ERROR
// EXECUTE PREPARED STATEMENT
if (!$ret_val) { chk_ifx_err1($statmt_id); }
// CHECK FOR ERROR
ifx_htmltbl_result ($statmt_id,
"BORDER='1' CELLSPACING=0 CELLPADDING=2 BGCOLOR='cornsilk' ALIGN=center");
?>
See output on next slide
PRS
Technologies, Inc.
October, 2000
Demo 6
12
Result from using: ifx_htmltbl_result
Tape_number
Title
1
A Piece of the Action
9
A Taste of Armageddon
10
Amok Time
2
And The Children Shall Lead
8
Arena
PRS
Technologies, Inc.
October, 2000
Demo 6
13
Selecting rows using a cursor
slide 1 of 2
<?php
$statmt_txt = "select * from vhs where type_code = 'ST1' order by title";
$statmt_id = ifx_prepare($statmt_txt,$connect_id,IFX_SCROLL);
if (!$statmt_id) { chk_ifx_err1($statmt_id); }
$ret_val = ifx_do($statmt_id);
// PREPARE
// CHECK FOR ERROR
// EXECUTE PREPARED STATEMENT
if (!$ret_val) { chk_ifx_err1($statmt_id); }
// CHECK FOR ERROR
echo <table border=5 cellspacing=0 cellpadding=5 bgcolor=cornsilk>”; // HTML
--- Continued on next slide ---
PRS
Technologies, Inc.
October, 2000
Demo 7
14
Selecting rows using a cursor
slide 2 of 2
while ($row = ifx_fetch_row($statmt_id, ”NEXT” )) {
$serial_id
$type_code
$tape_num
$title
$hours
$comment
= $row[serial_id];
= chop($row[type_code]);
= $row[tape_num];
= chop($row[title]);
= $row[hours];
= chop($row[comment]);
Create an
“associative” array
named “row”.
Populate fields from
associative array
Use column names
to identify fields.
echo "<tr>\n";
echo "<td><center>$serial_id</td>";
echo "<td><center>$type_code</td>";
echo "<td><center>$tape_num</td>";
echo "<td>$title</td>";
echo "<td>$hours</td>";
echo "<td>$comment</td>";
echo "<td>";
Use “chop” to
remove trailing
blanks if desired.
}
echo "</table>";
?>
--- Continued from previous slide ---
PRS
Technologies, Inc.
Demo 7
October, 2000
15
Can’t use placeholders
<?php
Invalid statement
$statmt_txt = "select * from vhs where type_code = ? order by title";
?>
“placeholders” are currently not supported, but
you can do this instead.
<?php
valid statement
$my_code = “ST1”;
$statmt_txt = "select * from vhs where type_code = ’ $my_code ' order by title";
?>
PRS
Technologies, Inc.
October, 2000
Demo 7
16
Variables from outside PHP
HTML Forms (GET and POST)
<html>
<form action="video_db2.php3" method="post">
Tape Number:
<input type="text" name=”tape_num">
<input type="submit">
</form>
</html>
<?php
echo “The tape number is: $tape_num <br>”;
?>
PRS
Technologies, Inc.
October, 2000
17
Inserting a row
<?php
if ($connect_id) {
$sql_txt = "insert into vhs (serial_id, type_code, tape_num, title) values ";
$sql_txt .= ” ($serial_id, \”$type_code\”, $tape_num, \”$title\”) ";
$ret_val = ifx_query($sql_txt,$connect_id);
// INSERT RECORD
if (!$ret_val) { chk_ifx_err1($statmt_id); }
// CHECK FOR ERROR
}
?>
PRS
Technologies, Inc.
October, 2000
18
Creating an associative array
<?php
$input_array = array (
"type_code"
=> $type_code,
"tape_num"
=> $tape_num,
"title"
=> $title,
"hours"
=> $hours,
"comment"
=> $comment
);
?>
An associative array is an array which uses a string (instead of a
number) to locate an element within the array.
PRS
Technologies, Inc.
October, 2000
19
Stepping through an associative array
<?php
$type_code_array = array (
"MOVIE"
"DS9"
"ST1"
"ST2"
"VOY"
"HOME"
"MAX"
"OTHER"
Create an associative array of type codes
=> "Movies",
=> "Deep Space 9",
=> "Star Trek (Original)",
=> "Star Trek the Next Generation",
=> "Star Trek Voyager",
=> "Home Movies",
=> "Max Headroom",
=> "Other");
echo “<select name='type_code'> <option value=' '>Choose Type of Video”;
while (list($key,$value) = each($type_code_array)) {
if ($key == $type_code) {
echo "<option value='$key' selected>$value\n";
} else {
echo "<option value='$key'>$value\n";
}
}
echo "</select>”;
?>
PRS
Technologies, Inc.
October, 2000
Step through
the array
20
Searching a string for a pattern
Perl-compatible Regular Expression functions
<?php
if (preg_match("/\|/",$query_string)) {
… your code here …
}
// Search $query_string for a pipe
// Do this if pipe was found
Search a string for the presence of a pipe symbol
?>
• Perform a regular expression match on a string.
• Return (true) if match was positive.
• The syntax for the pattern search closely resembles Perl.
PRS
Technologies, Inc.
October, 2000
21
Creating an array from a
pipe-delimited string
Perl-compatible Regular Expression functions
<?php
Create an array from pipe demimited string
$query_string = “ 123 | 456 | 789 “;
$pipe_list = preg_split("/\|/",$query_string);
// Create an array of strings
while ( list($key,$value) = each($pipe_list)) { // for each element in the array...
… your code here …
?>
• Split string by a regular expression.
• Returns an array containing substrings.
• The syntax closely resembles Perl.
PRS
Technologies, Inc.
October, 2000
22
Call a function
passing and returning variables
<?php
Return multiple values
Call a function
list($statmt_id,$cnt_found) = cnt_and_query("vhs",$where_clause);
?>
<?php
Start function
function cnt_and_query ($table_name,$where_clause) {
… more php code here …
return array ($statmt_id2,$num_rows_selected);
}
End function
?>
PRS
Technologies, Inc.
October, 2000
23
Breaking your program up
into logical modules
<?php
require("common.inc");
require("menu.inc");
require("display_form.inc");
require("query.inc");
require("add.inc");
require("modify.inc");
?>
• Use include() and require() to reference program modules.
• With include, statements are re-evaluated each time - almost
like calling a function.
• With require, statements are replaced by the required file
when it is first encountered. Good for user defined functions.
PRS
Technologies, Inc.
October, 2000
24
Scope of variables
• Scope spans the entire module, including “included” and
“required” files.
• Variables defined outside of a function, can be referenced
inside the function, if defined as globally available.
• Any variable defined inside a function can only be used
within that function, unless specifically defined as globally
available in that function.
global $variable5;
• Globals can also be accessed via the special “GLOBALS”
array.
$dbs = $GLOBALS["DATABASE"];
• See “demo8” for examples.
PRS
Technologies, Inc.
October, 2000
Demo 8
25
Installing PHP
• PHP (without Informix) is usually available on most Linux and
Apache installations using ODBC.
• Configuring PHP to use Informix is NOT a cakewalk.
• Requires use of ESQL/C libraries to compile (free w/Client SDK).
• Don’t reuse previous versions of Apache httpd.conf if you are
upgrading Apache from a earlier version.
• If you want the database to reside in a machine separate
from the web server, you will need Informix Client SDK or IConnect on the web server.
• If you're using I-Connect on the deployment machine, you
must compile on a machine with ClientSDK installed. And
CSDK and I-Connect need to be in the same directory on the
two different machines.
PRS
Technologies, Inc.
October, 2000
26
Installing PHP with Apache
Overview
• Aquire or download the software.
• Unpack Apache and PHP tar-balls.
• Set your Informix environmentals.
• Pre-Configure Apache.
• Configure, compile and install PHP
• Configure, compile and install Apache.
• Update Apache startup script with Informix environment
• Update Apache httpd.conf
• Re-start Apache web server.
• Test.
PRS
Technologies, Inc.
October, 2000
27
Installing PHP with Apache
Aquire or download the software
• Go to http://www.php.net
• Click on: downloads
• Click on: Complete Source Code
• PHP 4.0.2 - 29 August 2000
• Go to http://www.apache.org
• Click on apache server.
• Click on download.
• Click on apache_1.3.14.tar.gz
PRS
Technologies, Inc.
October, 2000
28
Installing PHP with Apache
Unpack Apache and PHP tar-balls
cd /usr/local
gunzip apache_1.3.14.tar.gz
tar xvf apache_1.3.14.tar
gunzip php-4.0.2.tar.gz
tar xvf php-4.0.2.tar
PRS
Technologies, Inc.
October, 2000
29
Installing PHP with Apache
Set your Informix environmentals
INFORMIXDIR=/opt/informix
INFORMIXSERVER=myserver_shm
ONCONFIG=onconfig.myserver
Optional
PATH=$PATH:$INFORMIXDIR/bin
LD_LIBRARY_PATH=$INFORMIXDIR/lib:$INFORMIXDIR/lib/esql
Don’t forget this one!
export INFORMIXDIR INFORMIXSERVER ONCONFIG LD_LIBRARY_PATH
PRS
Technologies, Inc.
October, 2000
30
Installing PHP with Apache
Pre-Configure Apache
cd apache_1.3.14
./configure --prefix=/usr/local/apache
cd ..
PRS
Technologies, Inc.
October, 2000
31
Installing PHP with Apache
Configure, compile and install PHP
cd php-4.0.2
./configure --with-apache=../apache_1.3.14 --with-informix=$INFORMIXDIR
make
make install
cd ..
PRS
Technologies, Inc.
October, 2000
32
Installing PHP with Apache
Configure, compile and install Apache
cd apache_1.3.14
./configure --prefix=/usr/local/apache \
--enable-module=rewrite \
--enable-shared=rewrite \
--enable-module=most \
--enable-shared=max \
--enable-module=so \
--activate-module=src/modules/php4/libphp4.a \
--enable-module=php4
make
make install
cd ..
PRS
Technologies, Inc.
October, 2000
33
Installing PHP with Apache
Update Apache startup script with Informix environment
cd /etc/rc.d/init.d
• On Linux, update the apache start-up script “httpd” to
include the Informix environment.
• Or use apachectl. (Varies by platform.)
PRS
Technologies, Inc.
October, 2000
34
Installing PHP with Apache
Update Apache httpd.conf
cd /usr/local/apache/conf
• Update the apache configuration file “httpd.conf”.
• If you have upgraded Apache from a different version,
do not use the previous “httpd.conf” file as a basis for the
new one.
AddModule mod_php4.c
<IfModule mod_php4.c>
AddType application/x-httpd-php4 .php4
AddType application/x-httpd-php4-source .phps
</IfModule>
PRS
Technologies, Inc.
October, 2000
35
Installing PHP with Apache
Re-start Apache web server
On Linux:
cd /etc/rc.d/init.d
./httpd restart
Also, you can use:
apachectl
apachectl configtest
PRS
Technologies, Inc.
October, 2000
36
PHP Resources
PRS Technologies, Inc.
• http://www.prstech.com (downloads section)
•
•
•
•
•
This presentation
Entire source of videotape demo
Source of all examples
Tech-Notes article on PHP by Mario Estrada
Monterey Peninsula Ski Club - Cabin Reservation System
by Jonathan Leffler
• http://www.php.net
• http://www.zend.com
PRS
Technologies, Inc.
October, 2000
37