Administration
  Home arrow Administration arrow Page 7 - Setting Up Database Driven Websites
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADMINISTRATION

Setting Up Database Driven Websites
By: Ying Zhang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    1999-05-20

    Table of Contents:
  • Setting Up Database Driven Websites
  • Requirements
  • Installing MySQL
  • Installing Apache
  • Installing PHP
  • Creating the Database
  • Making a PHP Script
  • Testing the Script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Setting Up Database Driven Websites - Making a PHP Script
    (Page 7 of 8 )

    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.

    index.php3

    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>
    add.php3

    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 Administration Articles
    More By Ying Zhang


     

       

    ADMINISTRATION ARTICLES

    - Configuring Load-Balanced Clusters
    - Load-Balanced Clusters
    - UNIX Time Format Demystified
    - Making Changes in the CVS
    - Building Your First CVS Repository
    - CVS Quickstart Guide
    - Authorizing Users in Samba
    - Handling User Accounts in Samba
    - Authentication in Samba
    - Accounts, Authentication, and Authorization
    - Advanced Concepts on Dealing with Files and ...
    - Dealing with Files and Filesystems
    - More Hacks for the User Environment in BSD
    - Personalizing the User Environment in BSD
    - Customizing the User Environment in BSD

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway