When you talk about PHP and databases, people tend to assumeyou're talking about MySQL. But hang on to your horses - difficultthough it may be to believe, PHP does include support for a number ofother databases. One of them is PostgreSQL, the *other* open-sourcedatabase - and this article tells you everything you need to know aboutusing it with PHP.
Finally, how about one more example to wrap things up? This next script contains a form which can be used to enter new addresses into the table, together with a form processor that actually creates and executes the INSERT statement.
// form not yet submitted
// display form
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input name="name" type="text" size="50">
<textarea name="address" rows="6" cols="40"></textarea>
<input name="tel" type="text" size="10">
<input name="email" type="text" size="30">
<input type="submit" name="submit" value="Add">
// form submitted
// prepare to insert data
// database access parameters
// alter this as per your configuration
$host = "localhost";
$user = "postgres";
$pass = "postgres";
$db = "test";
// open a connection to the database server
$connection = pg_connect("host=$host dbname=$db user=$user
die("Could not open connection to database server");
// error checks on form submission go here
// generate and execute a query
$query = "INSERT INTO addressbook VALUES
(nextval('addressbook_id_seq'), '$name', '$address', '$tel', '$email')";
$result = pg_query($connection, $query) or die("Error in query:
$query. " . pg_last_error($connection));
echo "Data successfully added.";
// close database connection
As you can see, this script is broken up into two main sections - the
appropriate section is displayed depending on whether or not the form has been submitted.
The first part merely displays an HTML form, with fields corresponding to the columns in the "addressbook" table. Once the user enters data into these fields, the same script is called again; this time, the second half will get executed. An SQL query is generated from the data entered into the form, and this query is executed using the pg_query() function you've become familiar with. A success message is displayed once the data has been successfully INSERTed.
If you're familiar with building MySQL-based Web applications with PHP, the procedure above should be familiar to you - the only difference lies in the functions used to communicate with the database server.