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.

-==-
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).

-==-
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:
-==-

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:mysqlin
(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.

-==-
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 =-
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.

Dragos Mincinoiu is a staff writer for Murdok.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top