close

Head First PHP & MySQL

Head First PHP & MySQL

If you're ready to create web pages more complex than those you can build with HTML and CSS, Head First PHP & MySQL is the ultimate learning guide to building dynamic, database-driven websites using PHP and MySQL. Packed with real-world examples, this book teaches you all the essentials of server-side programming, from the fundamentals of PHP and MySQL coding to advanced topics such as form validation, session IDs, cookies, database queries and joins, file I/O operations, content management, and more.

Head First PHP & MySQL offers the same visually rich format that's turned every title in the Head First series into a bestseller, with plenty of exercises, quizzes, puzzles, and other interactive features to help you retain what you've learned.

  • Use PHP to transform static HTML pages into dynamic web sites
  • Create and populate your own MySQL database tables, and work with data stored in files
  • Perform sophisticated MySQL queries with joins, and refine your results with LIMIT and ORDER BY
  • Use cookies and sessions to track visitors' login information and personalize the site for users
  • Protect your data from SQL injection attacks
  • Use regular expressions to validate information on forms
  • Dynamically display text based on session info and create images on the fly
  • Pull syndicated data from other sites using PHP and XML

Throughout the book, you'll build sophisticated examples — including a mailing list, a job board, and an online dating site — to help you learn how to harness the power of PHP and MySQL in a variety of contexts. If you're ready to build a truly dynamic website, Head First PHP & MySQL is the ideal way to get going.

A collection of images from inside the book.

Table of Contents

1. It's Alive: Add Life to Your Static Pages
2. How It Fits Together: Connecting to MySQL
3. Creating Your Own Data: Create and Populate a Database
4. Your Application on the Web: Realistic and Practical Applications
5. When a Database Just Isn't Enough: Working With Data Stored in Files
6. Assume They're All Out to Get You: Securing Your Application
7. Remember Me?: Building Personalized Web Apps
7.5. Sharing is Caring: Eliminate Duplicate Code
8. Harvesting Data: Control Your Data, Control Your World
9. Better Living Through Functions: String and Custom Functions
10. Rules for Replacement: Regular Expressions
11. Drawing Dynamic Graphics: Visualizing Your Data... and More!
12. Interfacing to the World: Syndication and Web Services
i. The Top Ten Topics (We Didn't Cover): Leftovers
ii. A Place to Play: Set Up a Development Environment
iii. Get Even More: Extend Your PHP

Read Me:
What you should know before reading this book

This is a learning experience, not a reference book. We deliberately stripped out everything that might get in the way of learning whatever it is we're working on at that point in the book. And the first time through, you need to begin at the beginning, because the book makes assumptions about what you've already seen and learned.

We begin by teaching simple programming concepts and database connection basics, then more complicated PHP functions and MySQL statements, and finally more complex application concepts.

While it's important to create applications that allow users to add data to and retrieve data from your web application, before you can do that you need to understand the syntax of both PHP and MySQL. So we begin by giving you PHP and MySQL statements that you can actually try yourself. That way you can immediately do something with PHP and MySQL, and you will begin to get excited about them. Then, a bit later in the book, we show you good application and database design practices. By then you'll have a solid grasp of the syntax you need, and can focus on learning the concepts.

We don't cover every PHP and MySQL statement, function, or keyword.

While we could have put every single PHP and MySQL statement, function, and keyword in this book, we thought you'd prefer to have a reasonably liftable book that would teach you the most important statements, functions, and keywords. We give you the ones you need to know, the ones you'll use 95 percent of the time. And when you're done with this book, you'll have the confidence to go look up that function you need to finish off that kick-ass application you just wrote.

We support PHP 5 and MySQL 5.0.

Because so many people still use PHP 4 or 5, we avoid any PHP 4, 5, or 6 specific code wherever possible. We suggest you use PHP 5 or 6 and MySQL 5 or 6 while learning the concepts in this book. In developing this book, we focused on PHP 5 and MySQL 5, while making sure our code was compatible with later versions.

You need a web server that supports PHP.

PHP has to be run through a web server to work correctly. You need Apache or some other web server installed on your local machine or a machine to which you have some access so that you can run MySQL commands on the data. Check out Appendixes ii and iii for instructions on how to install and extend PHP and MySQL.

We use MySQL.

While there's Standard SQL language, in this book we focus on the particular syntax of MySQL. With only a few syntax changes, the code in this book should work with Oracle, MS SQL Server, PostgreSQL, DB2, and quite a few more Relational Database Management Systems (RDBMSs) out there. You'll need to look up the particular PHP functions and syntax if you want to connect to these other RDBMSs. If we covered every variation in syntax for every command in the book, this book would have many more pages. We like trees, so we're focusing on MySQL.

Code Downloads

Download all the code from the book here, or get it by chapter:

1. It's Alive: Add Life to Your Static Pages
2. How It Fits Together: Connecting to MySQL
3. Creating Your Own Data: Create and Populate a Database
4. Your Application on the Web: Realistic and Practical Applications
5. When a Database Just Isn't Enough: Working With Data Stored in Files
6. Assume They're All Out to Get You: Securing Your Application
7. Remember Me?: Building Personalized Web Apps
7.5. Sharing is Caring: Eliminate Duplicate Code
8. Harvesting Data: Control Your Data, Control Your World
9. Better Living Through Functions: String and Custom Functions
10. Rules for Replacement: Regular Expressions
11. Drawing Dynamic Graphics: Visualizing Your Data... and More!
12. Interfacing to the World: Syndication and Web Services

Example Web Sites

Make Me Elvis (This web property is no longer maintained by O'Reilly)

Advance Reviews

"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
Order
(opens a new window)

Buy 2 books, get 1 free!

Buy 2 books and get the third free! Use the coupon code OPC10 when you check out.

Who is this book for?

If you can answer "yes" to all of these, this book is for you:

  • Are you a web designer with HTML or XHTML experience and a desire to take your web pages to the next level?

  • Do you want to go beyond simple HTML pages to learn, understand, and remember how to use PHP and MySQL to build web applications?
  • Do you prefer stimulating dinner party conversation to dry, dull, academic lectures?

You should probably back away from this book if you can answer "yes" to any of these:

  • Are you completely unfamiliar with basic programming concepts like variables and loops? (But even if you've never programmed before, you'll probably be able to get the key concepts you need from this book.)
  • Are you a kick-butt PHP web developer looking for a reference book?
  • Are you afraid to try something different? Would you rather have a root canal than mix stripes with plaid? Do you believe that a technical book can't be serious if it creates an alien abduction database?

Forum

Ask the authors questions and chat with fellow readers in the Head First PHP & MySQL Forum.