Home arrow Site Administration arrow Page 7 - Setting Up Database Driven Websites

Making a PHP Script - Administration

This tutorial is an introductory guide to get you started in the world of server-side-scripting and web databases. It covers installation and configuration of MySQL, Apache, and PHP. An example script is also included as a guide for making your own server-side-scripts.

  1. Setting Up Database Driven Websites
  2. Requirements
  3. Installing MySQL
  4. Installing Apache
  5. Installing PHP
  6. Creating the Database
  7. Making a PHP Script
  8. Testing the Script
By: Ying Zhang
Rating: starstarstarstarstar / 17
May 20, 1999

print this article



We've created the database, now let's make the PHP scripts that form the guts of our web database example.

Creating the PHP Scripts

To keep things really simple, we will just create two scripts: one that lists all the entries in the database, and one that allows us to add new entries.


Create a new directory called example in your web directory:

# cd /home/httpd/htdocs # mkdir example

Next, create a file called index.php3 in this directory. It should contain:

<html> <head><title>Web Database Sample Index</title> </head> <body bgcolor=#ffffff> <h1>Data from mytable</h1> <? mysql_connect("localhost", "webuser", ""); $query = "SELECT name, phone FROM mytable"; $result = mysql_db_query("example", $query); if ($result) { echo "Found these entries in the database:<ul>"; while ($r = mysql_fetch_array($result)) { $name = $r["name"]; $phone = $r["phone"]; echo "<li>$name, $phone"; } echo "</ul>"; } else { echo "No data."; } mysql_free_result($result); ?> <p><a href="add.php3">Add new entry</a> </body> </html>

Next, we create add.php3 in the same directory. This script does two things, first it will prompt the user for information to add to the database. Second, it will add this information to the database. This second function is normally put in a separate file, but it is so easy to do that we cram them both into one PHP script:

<html> <head><title>Web Database Sample Inserting</title> </head> <body bgcolor=#ffffff> <? if (isset($name) && isset($phone)) { mysql_connect("localhost", "webuser", ""); $query = "INSERT INTO mytable VALUES ('$name', '$phone')"; $result = mysql_db_query("example", $query); if ($result) { echo "<p>$name was added to the database</p>"; } } ?> <h1>Add an entry</h1> <form> Name: <input type=text name='name'><br> Phone: <input type=text name='phone'><br> <input type=submit> </form> <p><a href="index.php3">Back to index</a> </body> </html>

That's it, nice and simple. Now let's test it.

>>> More Site Administration Articles          >>> More By Ying Zhang

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates


Dev Shed Tutorial Topics: