Table of Contents
| Introduction | 1 |
| Style Conventions | 2 |
| Support/Feedback | 3 |
| Web Support | 3 |
| Chapter 1 | What Is PHP? | 5 |
| What Is PHP and What Is It for? | 5 |
| Introducing Dreamweaver MX | 12 |
| Dreamweaver MX and PHP | 20 |
| Installing and Configuring PHP | 21 |
| Summary | 28 |
| Chapter 2 | What Is MySQL? | 31 |
| What Is MySQL? | 31 |
| Installing and Configuring MySQL | 33 |
| MySQL Basics | 35 |
| The MySQL Console | 35 |
| Introduction To Databases and SQL | 35 |
| Selecting the Working Database | 37 |
| Tables in MySQL | 38 |
| Configuring MySQL | 40 |
| Additional Tools for MySQL | 46 |
| MyCC | 46 |
| PHPMyAdmin | 49 |
| Summary | 53 |
| Chapter 3 | Getting Started with Dreamweaver MX | 55 |
| The Essentials | 55 |
| Creating a Site Definition | 56 |
| Site Definition Wizard--A Step-By-Step Guide | 56 |
| Advanced Site Definition--An Overview | 65 |
| Creating a Database Connection | 70 |
| Summary | 72 |
| Chapter 4 | Planning the Web Site | 75 |
| Architecture of a Dynamic Web Site | 76 |
| The Brief | 76 |
| Determining Site Features | 77 |
| Planning the Database Structure | 78 |
| Determining Site Functionality and Features | 80 |
| Summary | 88 |
| Chapter 5 | Beginning Site Development | 91 |
| Setting Up the Database | 91 |
| The bookings Table | 91 |
| The clients Table | 92 |
| The room Table | 93 |
| The users Table | 94 |
| Creating the Tables | 94 |
| Using Templates To Standardize Layout | 102 |
| Summary | 115 |
| Chapter 6 | Manipulating the Database | 117 |
| Database Operations within Dreamweaver MX | 117 |
| The Hotel Booking System | 117 |
| Making a Booking | 118 |
| Changing a Booking | 119 |
| Canceling a Booking | 120 |
| Building the Pages | 120 |
| Creating the Bookings Pages | 121 |
| Changing a Booking | 146 |
| Canceling a Booking | 159 |
| Editing the Template--Linking the Pages | 162 |
| Suggestions for Expansion | 164 |
| Summary | 165 |
| Chapter 7 | Advanced SQL Usage | 167 |
| Creating an Advanced Recordset | 167 |
| The Select Statement | 168 |
| Using Distinct | 168 |
| Choosing Columns | 169 |
| Tables, Joining Tables, and Foreign Keys | 172 |
| Filtering Rows | 178 |
| Grouping | 182 |
| Filtering Using Having | 187 |
| Sorting the Recordset | 187 |
| Limiting the Number of Rows | 188 |
| Modifying the Structure of Existing Tables | 190 |
| The Alter Table Syntax | 190 |
| Optimization | 191 |
| Summary | 195 |
| Chapter 8 | Creating a Search System | 199 |
| Search Engines | 199 |
| Designing the User Interface | 201 |
| Building Your Page | 201 |
| Retrieving the Value Entered By the User | 203 |
| Building the Query | 204 |
| The Base Query | 205 |
| Creating the Dynamic Query | 206 |
| Displaying the Results | 212 |
| Summary | 215 |
| Chapter 9 | Hand Coding within Dreamweaver MX | 217 |
| PHP Code Syntax | 217 |
| Statements | 218 |
| Variables | 218 |
| Control Structures | 221 |
| Functions | 223 |
| PHP Resources | 223 |
| Why Hand Code within Dreamweaver MX? | 224 |
| Coding Options in Dreamweaver MX | 224 |
| Code View and Code Inspector | 224 |
| Code Options in Preferences Window | 225 |
| New Hand Coding Features in MX | 227 |
| Good Hand Coding Practices | 240 |
| Code Options | 241 |
| Indenting Code | 241 |
| Commenting Your Code | 242 |
| Naming Variables | 243 |
| Balancing Braces | 244 |
| Practical Example--Login System for Dreamweaver Hotel | 246 |
| Overview of Our Login System | 246 |
| The Database Users Table | 247 |
| Create Users Page--The Layout | 247 |
| The PHP Include File | 249 |
| Create Users Page--Adding the Code | 252 |
| The Login Page | 256 |
| The Menu Page | 260 |
| Create Users Page--Security | 261 |
| Summary | 262 |
| Chapter 10 | The Server Behavior Builder | 265 |
| What Is the Server Behavior Builder? | 265 |
| When To Use the Server Behavior Builder | 268 |
| How To Build a Server Behavior | 268 |
| Starting the Server Behavior Builder | 269 |
| The Code Blocks | 269 |
| Building an Interface for Our Server Behavior | 276 |
| Finishing the Server Behavior | 280 |
| How To Copy a Server Behavior | 281 |
| Summary | 282 |
| Chapter 11 | Debugging Your Code | 285 |
| Programming Errors | 285 |
| Syntax Errors | 286 |
| Run-time Errors | 288 |
| Logic Errors | 289 |
| Error Types in PHP | 290 |
| Custom Error Handling | 296 |
| Viewing Your Server Settings | 305 |
| Debugging Techniques | 307 |
| Creating a Custom Debugging Function | 307 |
| Two Brains Are Better Than One | 314 |
| SQL Errors | 314 |
| Where To Seek Help | 314 |
| Macromedia Forums | 314 |
| Web Sites | 315 |
| Newsgroups | 315 |
| Summary | 316 |
| Index | 319 |
Read a Sample Chapter
Dreamweaver MX: PHP Web Development
By Gareth Downes-Powell Tim Green Bruno Mairlot
John Wiley & Sons
Copyright © 2003
Gareth Downes-Powell, Tim Green, Bruno Mairlot
All right reserved.
ISBN: 0-7645-4387-3
Chapter One
What Is PHP?
This chapter is a basic introduction to the workings of PHP and how it all began. We will briefly
discover its origins as a programming language, looking at how it has evolved in earlier versions of
Dreamweaver to its current implementation in Dreamweaver MX.
As an introductory guide, we will take you through your first steps in working with PHP. We'll look at
where you can obtain it, and discuss the various installation options that are available to you. We'll
also cover, in detail, the necessary steps to install and configure PHP to work with your preferred
web server.
Summarizing this chapter we also include timely troubleshooting hints and tips should you encounter
any problems or difficulties with your new PHP installation.
What Is PHP and What Is It for?
Put simply, PHP is a tool for creating dynamic web pages. Its presence iscompletely transparent to
the end user. PHP is easy to learn, and most importantly, it's easy to implement.
PHP in the Real World
So, PHP creates dynamic web pages, but what exactly do we mean bydynamic?
Today's web sites come in all shapes and sizes. Some provide rich user experiences using Flash
animations; others provide interactive page elements created with JavaScript, or a combination of
both (or other) technologies. These types of web site can be described as being dynamic as some
part of them changes as a result of either a manual or automatic stimulus. PHP is no different in this
respect, as it too, reacts to a series of stimuli in a pre determined way. However, there is one primary
difference between these two methods.
Dynamic pages created with JavaScript, or other similar technologies, rely on the ability of the web
site visitor's own computer to manifest their effects. This is called client-side scripting, the web
pages are sent via the web server to the visitor's web browser, and once loaded the scripts begin to
work. Because the code that generates these interactions is sent directly to the browser it is open to
public scrutiny, and is therefore not the best choice in all situations. A user authentication system, for
example, becomes ineffective because the code that handles the login process is completely visible,
and therefore insecure.
PHP, however, works in an entirely different way. A web page containing PHP code is "pre-processed"
by the PHP engine, called an interpreter, and the results of this processing are passed
back to the web server and on to the visitor's web browser. As only the results of the PHP
processing are sent to the browser, the code that generated them remains hidden, and is therefore
much more secure. This kind of pre-processing is called server-side scripting, and while it doesn't
provide the same kind of dynamic effects as JavaScript, PHP pages can be called dynamic. Given
the example above, of a user authentication system: one page of PHP code, can process a
username and password, determine if these values are valid or invalid and based on this
determination, send the user to a login failure page, or to a successful login page.
Dreamweaver MX allows us to harness the power of PHP in a visual way, without delving into the
code itself, unless we want to. Because of this, Dreamweaver MX is perfectly suited to the rapid
development of dynamic web sites. To better illustrate this throughout the book we will base all our
examples around a single case study, which will allow you to work through each chapter and watch
the whole process of dynamic web development evolve.
The case study chosen for this book is that of a hotel booking system. This is a good example to use
as it allows you to view the process from the perspectives of both the hotel staff and the customer.
Both parties will interact with the web site in totally different ways. The customer will see the web site
only via its public interface, where they will be able to find out more about the hotel itself, and make
room reservations. The hotel staff, however, will have additional access to a private administration
area where they will be able to view the customer bookings and more.
The Hotel Booking System
Booking a hotel room is now a very common practice on the Internet. It is quick and easy. To book a
hotel room, the customer visits the web site, enters the date of arrival, date of departure, and the
type of room required. Using this information, the web site searches the reservations database to
ensure that there is an appropriate room available for the dates specified. When the search is
complete the customer's reservation is either denied (if no rooms are available), or confirmed in
which case the customer then proceeds to enter their credit card details to secure the booking.
As you can see from the above example, this is a type of dynamic web site. The customer provides
the stimulus (the dates of the stay, and the type of room required), and the web site dynamically
determines whether those requirements can be met. This type of web site is more commonly known
as a database-enabled web site, as all of the information that the web site needs is stored on a
database. Through the magic of PHP this information is retrieved, searched, sorted, and evaluated to
determine the appropriate response.
Throughout this book, we will return to this example, and explore elements of it in more detail. We
will look at the development tasks required to develop just such a system. All of the principles used
in the book are applicable, regardless of the type of project you are working on.
A Brief History of PHP
From its earliest beginnings PHP was designed for environment-independent development. Able to
run on many different web servers and operating systems, it is an easy-to-learn and very flexible
development tool.
Born from a series of Perl (or CGI - Common Gateway Interface) scripts written by Rasmus
Lerdorf in 1995, PHP was initially two independent tools. Personal Home Page Tools was written
purely to track the number of visits to the author's online resume. To increase performance and
provide greater stability, these scripts were soon rewritten in C. The functionality they provided
soon attracted the attention of other developers using the same web server. Rasmus allowed
these developers access to his scripts, which proved to be so popular that he was soon receiving
requests for additional features. Rasmus was also interested in the use of databases wherever
possible, and had developed another series of Perl scripts called Form Interpreter (FI). These
scripts processed requests to databases and routed the information back to a web page, to create
interactive, dynamic forms.
Soon, both PHP and FI were merged into a single unified tool. This tool was called PHP/FI v2, and
was the forerunner of PHP as we know it today.
In 1997, as the development of PHP/FI continued to escalate, it became apparent that the project
was becoming larger than one man could handle and the development of PHP was opened up to a
group of developers. Two members of this group, Zeev Suraski and Andi Gutmans were responsible
for a complete rewrite of the core PHP code, which became known as the Zend engine, a name
derived from both author's names. This engine set out the architecture and functionality for all future
versions of PHP.
Along the way the meaning of PHP itself also changed; it became a recursive acronym, one where
the acronym's meaning refers to itself. PHP now stood for "PHP: Hypertext Preprocessor".
The power and flexibility offered by PHP 3 grabbed the attention of the web professional, and PHP
became a more mainstream development option. Due to this huge increase in popularity, the Zend
Engine was rewritten once again, with further enhancements and functionality. This final rewrite
forms the basis of the current PHP versions, version 4.
Before looking more closely at PHP, it really is only fair if we take a step back and look at the
other options available. PHP is by no means unique in what it does, in fact there is a whole wealth
of different technologies available, and like PHP, all have their strengths and weaknesses. Some
of these technologies are open source, or free for developers to use in non-production
environments, whilst others require you to purchase a license before you can begin to learn about
them. In fact, there are so many different options that delving deeper into this subject will reveal
quite a number of possibilities!
Rather than discuss all of these available options, we shall, for the purpose of this book, concentrate
solely on the other scripting languages that are supported by Dreamweaver MX.
The Alternatives
With everything in life, no matter what you decide to do, there is always another way to achieve the
same goal. Creating a database-enabled web site is no exception. All of the different technologies
available achieve, ultimately the same thing, but they all do it slightly differently.
The main alternatives are Active Server Pages (ASP), Java Server Pages (JSP) and Cold Fusion
Markup Language (CFML). There are, of course, others, but these four are the main contenders in
terms of popularity (of course, all of these alternatives are also natively supported within Dreamweaver
MX). All of these technologies have their own strengths and weaknesses, as does PHP.
Whilst the majority of alternatives are commercial in nature, some are more developer-friendly than
others, providing free development-only versions, which are useful for testing and learning. However,
in a production environment, there are usually costs associated with the installation of the relevant
production-level server.
ASP, for example, requires that you purchase a version of Windows that comes with Internet
Information Server (IIS). Windows NT Server, Windows 2000, and Windows XP Professional all
come with a version of IIS, but it can prove to be an expensive option when installing a production
server of your own. There are alternatives, however, so that ASP pages can be used on other web
servers, such as Apache. Amongst these are ChilliSoft ASP, and iASP, both of which are fairly
popular. Though still commercial in nature they do provide a cheaper alternative and do not commit
you to the use of a Windows-based web server.
ASP has proven to be a popular choice amongst developers, as there are many relatively cheap
hosting companies that provide ASP hosting via a shared server, where multiple users and web sites
exist on the same machine. In such an instance, you are not required to buy any form of Windows,
and ASP really does become a viable option.
ASP.NET is similar in nature to ASP, in that you must have an appropriate Windows-based operating
system and web server to process ASP.NET pages. However, Halcyon Software, the developers of
iASP, are currently working on a version of the .NET framework called iNET, for use on alternative web
servers. This will ultimately lead to another commercial program, but does open up more possibilities
for development. For more information on iNET see halcyonsoft.com/products/iNET.asp. As
the popularity of ASP.NET grows, it will undoubtedly become just as viable an option as ASP. As more
hosting companies provide ASP.NET support via a shared server, the price of ASP.NET hosting will
reduce dramatically.
JSP is slightly different in that there are both commercial versions of JSP as well as open source
versions. This is good as you can experiment with versions of JSP, to discover which one suits you
best, and as you aren't tied to one particular operating system, as there are versions of JSP
available for most forms of web server. Unfortunately, JSP pages haven't proved to be a popular
option, not many hosting companies provide JSP support, and those that do usually charge a
premium for the service.
CFML is a totally commercial server option. If you wish to install ColdFusion onto your production
server, then you have to purchase the ColdFusion software, which is available in Professional and
Enterprise versions depending on your requirements. Whilst there are flavors of ColdFusion that
will work on a number of different web servers, it hasn't proven to be a popular option amongst
host providers either, as even in a shared server environment it is a costly option. If you wish to
use ColdFusion locally, for development or learning purposes, there is a free development version
available, but this version is feature-limited to prevent more than one user visiting the generated
web pages.
PHP, in contrast, is completely free to use either privately or commercially, and for this reason there
are no associated costs in running PHP on your own web server. PHP, like some of the other server
technologies, can also be installed on a number of different web servers, and due to its easy
installation, and lack of price tag, it has proven a very popular choice amongst host providers.
ASP
Active Server Pages is a proprietary technology, based around Microsoft's Internet Information
Server. It allows you to combine HTML, Visual Basic Script (VBScript), and JavaScript with reusable
ActiveX Components to create dynamic web pages.
For many developers this presents the ultimate in dynamic web page development because of the
tight integration and interoperability between all of the components. Historically, ASP has proven to
be costly to implement, due in part to its reliance on other Microsoft products. However, this is no
longer a real issue, thanks to the large number of companies offering cheap ASP hosting packages.
In comparison to PHP, ASP can be unnecessarily unwieldy and slow. Much of the functionality that
PHP offers as standard is not integrated into ASP's core code, which brings about a reliance on
third-party code to extend and enhance it. ASP can present a very steep learning curve, especially to
the first-time developer.
ASP.NET
Like ASP, ASP.NET is another proprietary technology, built around Microsoft's Internet Information
Server and its own server variations called the .NET Web Server. Whilst ASP.NET is a relatively new
kid on the block, it is a more developer-friendly language and is specifically geared towards the rapid
development of web applications.
To achieve this, it provides the flexibility of allowing you to decide which programming language you wish
to work with, with support for 25 different programming languages. It provides a wealth of new
functionality, previously unavailable to ASP developers, and offers enhanced performance and reliability.
ASP.NET really is more of a programming language than a scripting language, and for this reason it
is attracting more and more application developers every day.
Unfortunately, as ASP.NET is still relatively new, it is costly to implement in a production
environment. There are only a very limited number of companies providing .N
Continues...
Excerpted from Dreamweaver MX: PHP Web Development
by Gareth Downes-Powell Tim Green Bruno Mairlot
Copyright © 2003 by Gareth Downes-Powell, Tim Green, Bruno Mairlot.
Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.