Table Of ContentAdvance Praise for Head First PHP & MySQL
“PHP and MySQL are two of today’s most popular web development technologies, and this book shows
readers why. Building a site without them is now as unthinkable as doing web design without CSS. This
book is a great introduction and is laugh-out-loud funny. It’s the book I wish I had learned from.”
— Harvey Quamen, Associate Professor of English and Humanities Computing,
University of Alberta
“Everything we’ve come to accept about the drudgery of technical learning has been abandoned and in
its place an unusually fun method for learning is created. I have full confidence that the Head First series
will revolutionize the technical publishing industry, and that these new methods will be the eventual
standard. I bet my tech-phobic grandmother could pick up PHP and MySQL techniques after a single
reading. She’d probably even have a good time doing it!”
— Will Harris, Database Administrator, Powered By Geek
“Reading Head First PHP & MySQL is like taking a class from the ‘cool’ teacher. It makes you look
forward to learning.”
— Stephanie Liese, Web Developer
“Using images and humor the book is easy to digest and yet delivers real technical know-how.”
— Jereme Allen, Web Developer
“‘After a challenging, high-speed read-through and lots of quirky “Do This” projects, such as “My dog
was abducted by aliens” and the “Mismatch Dating Agency,” I can’t wait to add some real PHP power
to my web sites.”
— David Briggs, Software Engineer and Technical Author
Praise for Head First HTML with CSS & XHTML
“Eric and Elisabeth Freeman clearly know their stuff. As the Internet becomes more complex, inspired
construction of web pages becomes increasingly critical. Elegant design is at the core of every chapter here,
each concept conveyed with equal doses of pragmatism and wit.”
— Ken Goldstein, Executive Vice President & Managing Director, Disney Online
“The Web would be a much better place if every HTML author started off by reading this book.”
— L. David Baron, Technical Lead, Layout & CSS, Mozilla Corporation,
http://dbaron.org/
“I’ve been writing HTML and CSS for ten years now, and what used to be a long trial and error learning
process has now been reduced neatly into an engaging paperback. HTML used to be something you
could just hack away at until things looked okay on screen, but with the advent of web standards and
the movement towards accessibility, sloppy coding practice is not acceptable anymore... from a business
standpoint or a social responsibility standpoint. Head First HTML with CSS & XHTML teaches you how
to do things right from the beginning without making the whole process seem overwhelming. HTML,
when properly explained, is no more complicated than plain English, and the Freemans do an excellent
job of keeping every concept at eye-level.”
— Mike Davidson, President & CEO, Newsvine, Inc.
“Oh, great. You made an XHTML book simple enough a CEO can understand it. What will you
do next? Accounting simple enough my developer can understand it? Next thing you know we’ll be
collaborating as a team or something.”
—Janice Fraser, CEO, Adaptive Path
“This book has humor, and charm, but most importantly, it has heart. I know that sounds ridiculous
to say about a technical book, but I really sense that at its core, this book (or at least its authors) really
care that the reader learn the material. This comes across in the style, the language, and the techniques.
Learning – real understanding and comprehension – on the part of the reader is clearly top most in
the minds of the Freemans. And thank you, thank you, thank you, for the book’s strong, and sensible
advocacy of standards compliance. It’s great to see an entry level book, that I think will be widely read
and studied, campaign so eloquently and persuasively on behalf of the value of standards compliance in
web page code. I even found in here a few great arguments I had not thought of – ones I can remember
and use when I am asked – as I still am – ‘what’s the deal with compliance and why should we care?’
I’ll have more ammo now! I also liked that the book sprinkles in some basics about the mechanics of
actually getting a web page live - FTP, web server basics, file structures, etc.”
—Robert Neer, Director of Product Development, Movies.com
Praise for Head First JavaScript
“So practical and useful, and so well explained. This book does a great job of introducing a complete
newbie to JavaScript, and it’s another testament to Head First’s teaching style. Out of the other
JavaScript books, Head First JavaScript is great for learning, compared to other reference books the size of
a phone book.”
— Alex Lee, Student, University of Houston
“An excellent choice for the beginning JavaScript developer.”
— Fletcher Moore, Web Developer & Designer, Georgia Institute of Technology
“Yet another great book in the classic ‘Head First’ style.”
— TW Scannell
“JavaScript has long been the client-side engine that drives pages on the Web, but it has also long been
misunderstood and misused. With Head First JavaScript, Michael Morrison gives a straightforward and
easy-to-understand introduction of this language, removing any misunderstanding that ever existed and
showing how to most effectively use it to enhance your web pages.”
— Anthony T. Holdener III, Web applications developer, and the author of Ajax:
The Definitive Guide.
“A web page has three parts—content (HTML), appearance (CSS), and behaviour (JavaScript). Head First
HTML introduced the first two, and this book uses the same fun but practical approach to introduce
JavaScript. The fun way in which this book introduces JavaScript and the many ways in which it
reinforces the information so that you will not forget it makes this a perfect book for beginners to use to
start them on the road to making their web pages interactive.”
— Stephen Chapman, Owner Felgall Pty Ltd., JavaScript editor, about.com
“This is the book I’ve been looking for to recommend to my readers. It is simple enough for complete
beginners but includes enough depth to be useful to more advanced users. And it makes the process of
learning fun. This might just be the only JavaScript book you ever need.”
— Julie L Baumler, JavaScript Editor, BellaOnline.com
Other related books from O’Reilly
Learning PHP & MySQL
Web Database Applications with PHP and MySQL
Programming PHP
Learning MySQL
PHP in a Nutshell
PHP CookbookTM
PHP HacksTM
MySQL in a Nutshell
MySQL CookbookTM
Other books in O’Reilly’s Head First series
Head First JavaTM
Head First Object-Oriented Analysis and Design (OOA&D)
Head First HTML with CSS and XHTML
Head First Design Patterns
Head First Servlets and JSP
Head First EJB
Head First PMP
Head First SQL
Head First Software Development
Head First JavaScript
Head First Ajax
Head First Physics
Head First Statistics
Head First Rails
Head First Web Design
Head First Algebra
Head First PHP & MySQL
Wouldn’t it be dreamy if there
was a PHP & MySQL book that
made databases and server-side
web programming feel like a
match made in heaven? It’s
probably just a fantasy...
Lynn Beighley
Michael Morrison
Beijing • Cambridge • Kln • Sebastopol • Taipei • Tokyo
Head First PHP & MySQL
by Lynn Beighley and Michael Morrison
Copyright © 2009 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales
department: (800) 998-9938 or corporate@oreilly.com.
Series Creators: Kathy Sierra, Bert Bates
Series Editor: Brett D. McLaughlin
Editor: Sanders Kleinfeld
Design Editor: Louise Barr
Cover Designers: Louise Barr, Steve Fehler
Production Editor: Brittany Smith
Proofreader: Colleen Gorman Michael’s nephew Julien
Indexer: Julie Hawks generously lent his
Superman powers to help
Page Viewers: Julien and Drew get this book finished.
Printing History: Drew is, at this very
December 2008: First Edition. moment, installing
a new kitchen in
Lynn’s new old house.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations,
Head First PHP & MySQL, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
No hardwood floors, UFOs, Elvis look-alikes, or virtual guitars were harmed in the making of this book. But a
few broken hearts were mended thanks to some careful mismatching!
ISBN: 978-0-596-00630-3
[M]
For my parents, who frequently use web applications and are always
there for me.
- Lynn Beighley
To Rasmus Lerdorf, who single-handedly sparked the language that
would eventually become PHP as we know it now. Enduring proof
that it really only takes one person to lead us all down a new, more
enlightened path.
- Michael Morrison
the author(s)
Author(s) of Head First PHP & MySQL
Lynn Beighley
Michael Morrison
Lynn Beighley is a fiction writer stuck in a
technical book writer’s body. Upon discovering that Michael Morrison has been an enthusiastic
technical book writing actually paid real money, she contributor to the online world ever since he ran a BBS
learned to accept and enjoy it. After going back to on his Commodore 64 way back when being a nerd
school to get a Masters in Computer Science, she was far less cool than it is these days. A few thousand
worked for the acronyms NRL and LANL. Then she baud later, he still marvels at how far we’ve come, and
discovered Flash, and wrote her first bestseller. A victim how fast. Michael doesn’t run a BBS anymore, but he’s
of bad timing, she moved to Silicon Valley just before still very much involved in the modern equivalents and
the great crash. She spent several years working for the tools we use to build them. He spends most of his
Yahoo! and writing other books and training courses. “official” time writing about web-related technologies,
Finally giving in to her creative writing bent, she moved having authored or co-authored over fifty books ranging
to the New York area to get an MFA in Creative Writing. from mobile game programming to XML. He entered
Her Head First-style thesis was delivered to a packed the Head First foray with Head First JavaScript, and hasn’t
room of professors and fellow students. It was extremely looked back.
well received, and she finished her degree, finished
Head First SQL, and just finished Head First PHP & Michael is also the founder of Stalefish Labs (www.
MySQL. Whew! stalefishlabs.com), an entertainment company
specializing in games, toys, and interactive media. And
Lynn loves traveling, writing, and making up elaborate he’s been known to actually spend time offline (gasp!)
background stories about complete strangers. She’s a skateboarding, playing ice hockey, and hanging out next
little scared of UFOs. to his koi pond with his wife, Masheed. He even sleeps
every once in a while.
viii