Databases are central to many
web applications. A database can hold almost any collection of information you
may want to search and update, such as a user list, a product catalog, or recent
headlines. One reason why PHP is such a great web programming language is its
extensive database support. PHP can interact with (at last count) 17 different
databases, some relational and some not. The relational databases it can talk to
are DB++, FrontBase, Informix, Interbase, Ingres II, Microsoft SQL Server, mSQL,
MySQL, Oracle, Ovrimos SQL Server, PostgreSQL, SESAM, and Sybase. The nonrelational databases it can talk
to are dBase, filePro, HyperWave, and the DBM family of flat-file databases. It also has ODBC support, so even if your favorite database isn't in
the list, as long as it supports ODBC, you can use it with PHP.
If your data
storage needs are simple and you don't need to serve many users, you may be able
to use a plaintext file as a makeshift database. This is
discussed in Section
10.2. Text files require no special database software but are appropriate
only for lightly used, basic applications. A text file can't handle structured
data well; if your data changes a lot, it's inefficient to store it in a plain
file instead of a database.
DBM flat-file databases, discussed in Section
10.3, offer more robustness and efficiency than flat files but still limit
the structure of your data to key/value pairs. They scale better than plaintext
files, especially for read-only (or read-almost-always) data.
PHP really shines, though, when paired with a SQL database. This combination is used for most of the
recipes in this chapter. SQL databases can be complicated, but they are
extremely powerful. To use PHP with a particular SQL database, PHP must be
explicitly told to include support for that database when it is compiled. If PHP
is built to support dynamic module loading, the database support can also be
built as a dynamic module.
Many SQL examples in this chapter use a table of information
about Zodiac signs. The table's structure is:
CREATE TABLE zodiac ( id INT UNSIGNED NOT NULL, sign CHAR(11), symbol CHAR(13), planet CHAR(7), element CHAR(5), start_month TINYINT, start_day TINYINT, end_month TINYINT, end_day TINYINT, PRIMARY KEY(id) );
And the data in the table is:
INSERT INTO zodiac VALUES (1,'Aries','Ram','Mars','fire',3,21,4,19); INSERT INTO zodiac VALUES (2,'Taurus','Bull','Venus','earth',4,20,5,20); INSERT INTO zodiac VALUES (3,'Gemini','Twins','Mercury','air',5,21,6,21); INSERT INTO zodiac VALUES (4,'Cancer','Crab','Moon','water',6,22,7,22); INSERT INTO zodiac VALUES (5,'Leo','Lion','Sun','fire',7,23,8,22); INSERT INTO zodiac VALUES (6,'Virgo','Virgin','Mercury','earth',8,23,9,22); INSERT INTO zodiac VALUES (7,'Libra','Scales','Venus','air',9,23,10,23); INSERT INTO zodiac VALUES (8,'Scorpio','Scorpion','Mars','water',20,24,11,21); INSERT INTO zodiac VALUES (9,'Sagittarius','Archer','Jupiter','fire',11,22,12,21); INSERT INTO zodiac VALUES (10,'Capricorn','Goat','Saturn','earth',12,22,1,19); INSERT INTO zodiac VALUES (11,'Aquarius','Water Carrier','Uranus','air',1,20,2,18); INSERT INTO zodiac VALUES (12,'Pisces','Fishes','Neptune','water',2,19,3,20);
The specific functions required to talk
to the database differ with each database, but each follows a similar pattern.
Connecting to the database returns a database connection handle. You use the connection handle
to create statement handles, which are associated with
particular queries. A query statement handle then gets the results of that
query.
if (! $dbh = OCILogon('david', 'foo!bar','ORAINST')) {
die("Can't connect: ".OCIError());
}
if (! $sth = OCIParse($dbh,'SELECT * FROM zodiac')) {
die("Can't parse query: ".OCIError());
}
if (! OCIExecute($sth)) {
die("Can't execute query: ".OCIError());
}

