Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > SCRIPT WEB DATABASES QUICKLY WITH PHP


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Script Web databases quickly with PHP
By Craig Knudsen - 1999-09-01 Page:  1 2 3 4 5 6 7 8

Setting Up The Database

To keep this example simple on screen, the address book contains only the person's first name, last name, and e-mail address. If you download the sample code (see Resources), though, you'll find additional fields that make the application more useful.

This example uses MySQL, but you could use any database that PHP supports. If you use another database (such as Oracle or ODBC), you will need to adapt the listing because PHP's API is slightly different for each database. For example, for ODBC connections you would use odbc_pconnect instead of connecting with the mysql_pconnect function.

Note that I've used MySQL's auto-increment feature in these examples. It's a handy way to generate unique integer keys for a table column. You can do the same thing with other databases by using stored procedures and triggers.

First, you must set up a valid MySQL login and password to use PHP. See the MySQL documentation for details (see Resources).

Then use the SQL commands in Listing 3 to set up the PERSON table within a database called ADDRESS_BOOK and add a single entry into the database. Enter the text using the mysql command-line program that is part of the MySQL distribution.

Listing 3. The commands for setting up the MySQL database and for entering the first contact



Listing 3
 1   CREATE DATABASE ADDRESS_BOOK;
 2   
 3   USE ADDRESS_BOOK;
 4   
 5   CREATE TABLE PERSON (
 6     ID int(10) DEFAULT '0' NOT NULL AUTO_INCREMENT,
 7     LAST_NAME VARCHAR(50) NOT NULL,
 8     FIRST_NAME VARCHAR(50) NULL,
 9     EMAIL VARCHAR(60) NULL,
10     PRIMARY KEY ( ID )
11   );
12   
13   INSERT INTO PERSON VALUES (
14     1, 'Knudsen', 'Craig', 'cknudsen@radix.net' );


View Script Web databases quickly with PHP Discussion

Page:  1 2 3 4 5 6 7 8 Next Page: Retrieving Data

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.