Tải bản đầy đủ (.pdf) (5 trang)

PHP 5/MySQL Programming- P5 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (123.69 KB, 5 trang )

Working with the Primary Key . . . . . . . . . . . . . . . . . . . . . 419
Recognizing Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . 419
Building the Foreign Key List Box . . . . . . . . . . . . . . . . . . 420
Working with Regular Fields . . . . . . . . . . . . . . . . . . . . . . 420
Committing a Record Update . . . . . . . . . . . . . . . . . . . . . 420
Deleting a Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Adding a Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Processing an Added Record. . . . . . . . . . . . . . . . . . . . . . 425
Building a List Box from a Field . . . . . . . . . . . . . . . . . . . 426
Creating a Button That Returns
Users to the Main Page . . . . . . . . . . . . . . . . . . . . . . . . 427
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Appendix A: Reviewing HTML and
Cascading Style Sheets . . . . . . . . .on cd
Appendix B: Using SQLite as an
Alternative Data Source . . . . . . . . .on cd
Index . . . . . . . . . . . . . . . . . . . . . . . . . . .429
xx
C
o
n
ten
t
s
omputer programming has often been seen as a difficult and arcane
skill. Programming languages are difficult and complicated, out of the
typical person’s reach. However, the advent of the World Wide Web has
changed that to some extent. It’s reasonably easy to build and post a
Web page for the entire world to see. The language of the Web is reasonably simple,
and numerous applications are available to assist in the preparation of static
pages. At some point, every Web author begins to dream of pages that actually do


something useful. The simple HTML language that builds a page offers the tan-
talizing ability to build forms, but no way to work with the information that
users type into these forms.
Often, a developer has a database or some other dynamic information they wish
to somehow attach to a Web page. Even languages such as JavaScript are not sat-
isfying in these cases. The CGI interface was designed as an early solution to this
problem, but CGI itself can be confusing and the languages used with CGI (espe-
cially Perl) are very powerful, but confusing to beginners.
PHP is an amazing language. It is meant to work with Web servers, where it can
do the critical work of file management and database access. It is reasonably easy
to learn and understand, and can be embedded into Web pages. It is as powerful
as more-difficult languages, with a number of impressive extensions that add
new features to the language.
In this book, I teach you how to write computer programs. I do not expect you to
have any previous programming experience. You learn to program using the PHP
language. Although PHP itself is a very specialized language (designed to enhance
Web pages), the concepts you learn through this language can be extended to a
number of other programming environments.
Whenever possible, I use games as example programs. Each chapter begins by
demonstrating a simple game or diversion. I show you all the skills you need to write
that game through a series of simple example programs. At the end of the chapter I
show the game again, this time by looking at the code, which at that point you will
understand. Games are motivating and often present special challenges to the pro-
grammer. The concepts presented are just as applicable in real-world applications.
I
n
t
ro
d
u

c
t
i
o
n
C
xxii
I
n
t
r
o
d
u
c
t
i
o
n
This second edition adds new features and includes updates from the previous
edition of the book. Specifically, it includes new chapters on object-oriented
programming (OOP) and XML, as well as examples on using PHP to create content
management systems. I’ve updated the code to reflect improvements in PHP 5.0,
including the improved object model and XML tools, and the new SQLite data
access tools.
Programming is not a skill you can learn simply by reading about it. You have to
write code to really understand what’s going on. I encourage you to play along at
home. Look at the code on the accompanying CD. Run the programs yourself. Try
to modify the code and see how it works. Make new variations of the programs
to suit your own needs.

W
eb pages are interesting, but on their own they are simply documents. You
can use PHP to add code to your Web pages so they can do more. A scripting
language like PHP can convert your Web site from static document to an
interactive application. In this chapter you learn how to add basic PHP functionality to
your Web pages. You also learn how to do these things:
• Download and install Apache
• Download and install PHP
• Configure Apache to recognize PHP 5.0
• Configure PHP to run extensions used in this book (including MySQL and XML)
• Ensure PHP is on your system
• Run a basic diagnostic check of your PHP installation
• Add PHP code to a Web page
E
x
p
l
o
r
i
n
g
t
h
e
P
H
P
E
n

v
i
r
o
n
m
e
n
t
1
CHAPTER
Introducing the Tip of the Day Program
Your first program probably won’t win any Web awards, but it takes you beyond
what you can do with regular HTML. Figure 1.1 illustrates the Tip of the Day page,
which offers friendly, helpful advice.
You could write this kind of page without using a technology like PHP, but the
program is a little more sophisticated than it might look on the surface. The tip
isn’t actually embedded in the Web page at all, but it is stored in a completely
separate file. The program integrates this separate file into the HTML page. The
page owner can change the tip of the day very easily by editing the text file that
contains the tips.
2
P
H
P
5
/M
y
S
Q

L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i

n
n
e
r
FIGURE 1.1
The tip of the day
might look simple,
but it is a
technological
marvel. It features
HTML, cascading
style sheets, and
PHP code.
IN THE REAL WORLD
The Tip of the Day page illustrates one of the hottest concepts in Web program-
ming today: the content management system. This kind of structure allows
programmers to design a Web site’s general layout, but isolates the contents
from the page design. The page owners (who might not know how to modify a
Web page directly) can easily change a text file without risk of exposing the
code that holds the site together. You’ll learn how to build a full-blown content
management system in chapter 8, “XML and Content Management Systems.”

×