Database Connectivity - And Free Code!!

So you want to connect to a database? Connecting to databases has been one of the most sought after concepts in the ever changing web development industry. Database connectivity allows us to move beyond simple, static, HTML pages, into fully functional web based applications. If you have ever shopped, activated a cell phone, or done your banking online, chances are you have used a database driven website. Following is an introduction to database connectivity, and with a little further investigation, you will be building web based applications in no time!

The first thing you need to know when planning a database driven site is the platform your server is running on. The following example is in PHP, and connecting to a mySQL database. This is for use on a Unix server, and will need to have support for PHP and mySQL. If you do not know this information, check with your server administrator.

Next you will need to create the table in your database which your script will interact with. You will need to log into your mySQL administration area in order to do this. Contact your server admin for information on how to do this, as well as the name of your database. Your table can hold any information you would like. For our purposes we will use a simple table called "Technologies" with 3 fields: ID, Name, and Description. ID will be an autonumber field, and the primary key for the table. In autonumber fields the next consecutive integer will automatically be entered each time you enter a record. The primary key is the field in your table which will be used to identify a record, for example to number "1" would relate to a specific name and description.

Enter the following data into your table(the ID field will be numbered automatically remember??):

Name Description
HTML Hypertext Markup Language
ASP Active Server Pages
SQL Structured Query Language
JSP Java Server Pages
PHP Personal Home Pages

Following is the complete code to connect to your database and print the contents of your Technologies table.



#substitute your username and password here
$pass ="password";

#connect to mysql
or die("Connection failed");

#select database to work with-substitute name of your database here
#set variable to hold query string
$query = "SELECT * FROM Technologies";

#set variable to hold results of query
$result = mysql_query($query, $db)

#print error for failed query
or die("Bad query: ".mysql_error());

#continue getting and printing results until end of results
while ($row = mysql_fetch_array ($result)){
echo "Name: ".$row["Name"]."<br>");
echo "Description: ".$row["Description"]."

So, there you have it. You have connected to a database, and printed the contents of a table. You can find more on back end and database integrated programming, including free scripts for download at DesigNerds Open Source Project. For more about PHP, visit

Kaya Casper is the owner of DesigNerds web solutions and is responsible for the back end development for this site.


The Musician's PlaceTo Shop!
Instant Gift Certificates!

© 2001-2005 Issues Magazine.
All Rights Reserved.

Get 15 FREE prints!