Article
Build Your Own Database Driven Web Site Using PHP & MySQL, Part 1: Installation
Your First PHP Script
It would be unfair of me to help you install everything—but stop short of giving you a taste of what a PHP script looks like until Chapter 3: Introducing PHP. So here’s a little morsel to whet your appetite.
Open your favorite text or HTML editor and create a new file called today.php. Type this into the file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Today’s Date</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<p>Today’s date (according to this web server) is
<?php
echo date('l, F dS Y.');
?>
</p>
</body>
</html>
Editing PHP Scripts in Windows with Notepad
Windows users should note that, to save a file with a .php extension in Notepad, you’ll need to either select All Files as the file type, or surround the filename with quotes in the Save As dialog box; otherwise, Notepad will unhelpfully save the file as today.php.txt, which will fail to work.
Editing PHP Scripts in Mac OS X with TextEdit
Mac OS X users are advised to be careful when using TextEdit to edit .php files, as it saves them in Rich Text Format, with an invisible .rtf filename extension by default. To save a new .php file, you must first remember to convert the file to plain text by selecting Format > Make Plain Text (?+?+T) from the TextEdit menu.
TextEdit also has a nasty habit of mistaking existing .php files for HTML documents when opening them, and attempting to display them as formatted text. To avoid this, you must select the Ignore rich text commands checkbox in the Open dialog box.
Try a Free IDE!
As you can tell from the preceding warnings, the text editors provided with current operating systems are a little unsuitable for editing PHP scripts. There are a number of solid text editors and Integrated Development Environments (IDEs) with rich support for editing PHP scripts that you can download for free. Here are a few that work on Windows, Mac OS X, and Linux:
If you’d prefer to avoid typing out all the code, you can download this file—along with the rest of the code in this book—from the code archive. See the Preface for details on how to download the code archive.
Save the file, and move it to the web root directory of your local web server.
Where’s My Server’s Web Root Directory?
If you’re using an Apache server you installed manually, the web root directory is the htdocs directory within your Apache installation (that is, C:\Program Files\Apache Software Foundation\Apache2.2\htdocs on Windows, /usr/local/apache2/htdocs on Linux).
For Apache servers built into WampServer, the web root directory is the www directory within your WampServer directory. You can reach it quickly by selecting the www directory menu item from the WampServer menu in your Windows System Tray.
If the Apache server you’re using is built into Mac OS X, the web root directory is /Library/WebServer/Documents.
The Apache server built into MAMP has a web root directory in the htdocs folder inside the MAMP folder (/Applications/MAMP/htdocs). If you prefer using a different folder as your web root, you can change it on the Apache tab of the MAMP application’s Preferences.
Open your web browser of choice, and type http://localhost/today.php (or http://localhost:port/today.php if Apache is configured to run on a port other than the default of 80) into the address bar to view the file you just created. (If you installed Apache on Windows, you may have selected the option to run it on port 8080. If you’re using MAMP, it’s configured by default to run Apache on port 8888.)
You Must Type the URL
You might be used to previewing your web pages by double-clicking on them, or by using the File > Open… feature of your browser. These methods tell your browser to load the file directly from your computer’s hard drive, and so they’ll fail to work with PHP files.
As previously mentioned, PHP scripts require your web server to read and execute the PHP code they contain before sending the HTML code that’s generated to the browser. Only if you type the URL (http://localhost/today.php) will your browser request the file from your web server so that this can happen.
The figure below shows what the web page generated by your first PHP script should look like.
![]()
Neat, huh? If you use the View Source feature in your browser, all you’ll see is a regular HTML file with the date in it. The PHP code (everything between <?php and ?> in the code above) was interpreted by the web server and converted to normal text before it was sent to your browser. The beauty of PHP, and other server-side scripting languages, is that the web browser can remain ignorant—the web server does all the work!
Be reassured also that before too long you’ll know code (like this example) as well as the back of your hand.
If the date is missing, or if your browser prompts you to download the PHP file instead of displaying it, then something is wrong with your web server’s PHP support. If you can, use View Source in your browser to look at the code of the page. You’ll probably see the PHP code right there in the page. Since the browser fails to understand PHP, it just sees <?php … ?> as one long, invalid HTML tag, which it ignores. Double-check that you have requested the file from your web server rather than your hard disk (that is, make sure the location bar in your browser shows a URL beginning with http://localhost), and make sure that PHP support has been properly installed on your web server using the instructions provided earlier in this chapter.
Full Toolbox, Dirty Hands
You should now be fully equipped with a web server that supports PHP scripts, a MySQL database server, and a basic understanding of how to use each of these. You should even have dirtied your hands by writing and successfully testing your first PHP script!
If the today.php script was unsuccessful for you, drop by the SitePoint Forums and we’ll be glad to help you figure out the problem.
In Chapter 2: Introducing MySQL, you’ll learn the basics of relational databases and start working with MySQL. I’ll also introduce you to the language of database: Structured Query Language. If you’ve never worked with a database before, it’ll be a real eye-opener!