Come to our Home Page

 

An Introduction to PHP and MySQL

 

Setting up your own Windows e-commerce site may seem like a daunting task at first. There is quite a bit to know, though with a basic understanding of the underpinnings and a few good tools, you’ll be on your way in no time at all.
Apache, PHP, and MySQL are excellent tools if you’re planning to build an e-commerce Web site with database connectivity. In this article I’ll show you how to configure all three of these tools without laboring for hours over the documentation for each one. We’ll take a look at how to install Apache, PHP, and MySQL on Windows 98, though the process is similar for most Windows platforms.

I recommend the CGI version of PHP for Windows users. It’s stable and you can easily plug it into your Apache Web server. Downloading the source code offers many more customization options, but we won’t cover that here because you may run into difficulties with compiling it. Troubleshooting those problems is outside the scope of this article.

A basic knowledge of an object-oriented programming language like C++ will make understanding PHP easy. I chose JavaScript for client-side scripting and PHP for the server-side – the control structures in JavaScript and PHP are similar to those in C++. We will create a database in MySQL, populate the database, and run a few short queries on the database using PHP.

Installing and Configuring Apache
The first thing you’ll need to do is download Apache. You can get that here. Download the file to your hard drive, and run the executable file. Choose the “typical” install. the httpd.conf file - the master Apache configuration file In the text editor of your choice (note: Word does not preserve line wrapping, however, though Notepad works just fine), open httpd.conf ( "C:Program FilesApache GroupApacheconf""). Most of the lines will be commented out; very few lines of code are left uncommented. Search for a line starting with:
#ServerName
Remove the comment "#" and change it to:
ServerName localhost

The ServerName does not have to be “localhost”, and if you happen to have a real host you can supply its name instead. The commented area above ServerName explains your options in detail.

Save the file and start Apache from the start menu. After starting Apache, open your Web browser and type "http://localhost/" in the location bar to view the default installation page and verify that it's working. If you used a real ServerName instead of the default localhost, type that name in the location bar instead.

Return to the httpd.conf file and search for a line starting with:
DocumentRoot
Change it to:
DocumentRoot "C:/website"
Note: The name of the directory could be anything you want like C: php_files and it refers to an existing directory on C drive. If you want a new directory, create one and set that as DocumentRoot.
Search for a line starting with:
<Directory />

Change the whole group so it resembles the following:
<Directory />
Options FollowSymLinks ExecCGI
AllowOverride None
</Directory>

<Directory "C:/website">
Order allow,deny
Allow from all
</Directory>

Installing PHP
Follow these easy steps:
See below to download the PHP 4.1.1 zip package [4,953Kb] - 03 January 2002.

Extract the contents to C:php4

Rename the file called "php.ini-dist" to "php.ini" and move it to C:WINDOWS or wherever the rest of your *.ini files live.

Take the two files "Msvcrt.dll" and "php4ts.dll", and put them in C:WINDOWSSYSTEM , or wherever you usually put your *.dll files.

Go back to the Apache httpd.conf and make a few modifications to tell Apache what to do with *.php or *.phtml files (and how to do it).

With httpd.conf open in your text editor, search for:
ScriptAlias /cgi-bin/ "C:/Apache/cgi-bin/"

Add another ScriptAlias line to the end, just like this:
ScriptAlias /php4/ "C:/php4/"

Now find a section that looks like this:
#AddType application/x-httpd-php3 .phtml
#AddType application/x-httpd-php3-source .phps

Change the section should now look something like this:
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

Search for the following line:
# Format: Action handler-name /cgi-script/location
#

You need to include an Action line for your new file types, so that they are automatically sent through the PHP parser. To do this add the following line:
Action application/x-httpd-php /php4/php.exe

Save your httpd.conf file and start Apache again.

There's one good way to test your installation: use the phpinfo() function. Open a text editor and type this:
<? phpinfo() ?>

Save this file as phpinfo.php, and put it in C:website, then fire up your Web browser and go to "http://localhost/phpinfo.php" where you should see a long page of variables and their values. The phpinfo() function produces this page, which shows you your environment and your settings. This tells you that both Apache and PHP are installed and functioning correctly.

Installing MySQL
See below to download MySQL. The installation itself is very fast and does not require any special modifications. Just follow the installer and restart the computer. Start your web server (Apache) and familiarize yourself with MySQL. I’ll assume you know enough about SQL to understand the statements for creating tables and populating the database. The queries are going to be very simple - the goal is to learn how to manipulate the database using PHP.

Connecting to and creating a database
To create a database in MySql you have two choices: use DOS command prompt statements or use PHP code.

DOS
From a DOS prompt window switch to MySQL’s bin directory by typing:
cd c:mysql\bin
(assuming MySql was installed on drive C). To create a database in MySql you will have to type the following:

mysqladmin –u root create roll

This will create a database called “roll”.

PHP
In PHP we will use mysql_create_db () function to create the database.

If(mysql_create_db (“roll”, $link))
{
print (“The database, roll, was successfully created!<BR>”);
}

else
{
print (“The database, roll, could not be created!<BR>”);
}

Obviously, you’ll have to provide the database name and the link to MySql obtained using the mysql_connect() function:

$link = mysql_connect (“localhost”, “root”);

To close the link we will use the mysql_close() function.

mysql_close($link);

Unless you have logged in using a user account that does not have administrative privileges, this should work without any problems. The dump file used for the database will contain the following statements:

create table student
(
studentId int auto_increment not null,
firstName varchar(20) not null,
lastName varchar(20) null,
email varchar(20),
primary key(studentId),
unique id(studentId)
);
insert into student values(1, 'john', 'doe', 'johnd@hotmail.com');
insert into student values(2, 'jeff', 'bridges', 'jbridges@msn.com');

A dump file is an SQL script file. It contains well-formed SQL statements; it is preferable to the command line because it is easier to debug and reload if it contains errors. Dump files should be placed in MySQL’s bin directory. Load that up to the database by typing the following:
mysql –u root roll < roll.dump

Now you should have a database that’s ready to use. I suggest using a text editor that will preserve line wrapping like Notepad. Lets start with some simple PHP statements. PHP code can be inserted just about anywhere into your HTML code, or replace HTML altogether.

First of all you will need to connect to your database so we will use the following statement:
$link = mysql_connect (“localhost”, “root”);

The statement was written under the assumption that you are logged in as the root user. Now that you are connected, you will need to select a database with the following statement:
mysql_select_db (“roll”, $link);

Retrieving Data From A Database

Lets write a simple query:

$result = mysql_query(“SELECT * FROM customer”, $link);

The result of the query will be stored in the variable $result. To run a query on the database we can also use the mysql_db_query() function. The first parameter is the database name, second is the query string in SQL format, and the third is the link to the database.

mysql_db_query(“databaseName”, $Query, $link)

Again, we can use an if/else control structure to display errors during execution, which can be helpful for debugging.

if (mysql_db_query(“databaseName”, $Query, $link))
{
print (“The query was successfully executed!<BR>”);
}

else
{
print (“The query could not be executed!<BR>”);
}

Below is the content of a PHP file. Save that in your DocumentRoot directory (mine is C:website) and fire up the web browser. You should be able to see a table that displays the content of our database.

<html><body>

<?php

$link = mysql_connect("localhost", "root");

mysql_select_db("roll",$link);

$Result = mysql_query("SELECT * FROM student",$link);

echo "<table border=1>";
echo "<tr><td><b>Name</b></td><td><b>Email</b></tr>";

while ($Row = mysql_fetch_row($Result))
{
printf ("<tr><td>%s %s</td><td>%s</td></tr>",
$Row[1], $Row[2], $Row[3]);
}

echo "</table>";

?>
</body></html>

Now you have a basic understanding of how to create, populate, and retrieve information from a MySQL database using PHP. Feel free to modify the PHP file and insert it into or delete it from the database. For more information on other options to try, there’s plenty of online documentation available at www.php.net and other sites on the Web.









_____________________________________________________________________________________________________________