PHP and MySQL: The Missing Manual (2011)
But now you want more.
If any of these are the case—and you may find that all these are the case!—then learning PHP and MySQL is a great way to take a giant programming step forward. Even if you’ve never heard of PHP, you’ll find it’s the best way to go from building web pages to creating full-fledged web applications that store all sorts of information in databases. This book shows you how to do just that.
What Is PHP?
FREQUENTLY ASKED QUESTION: WHAT DOES PHP STAND FOR?
So now PHP stands for PHP: Hypertext Preprocessor. If that sounds geeky, it is. In fact, it’s a bit of a programmer joke: the acronym PHP stands for something that actually contains the acronym PHP within itself. That makes it a recursive acronym, or an acronym that references itself. You don’t have to know what a recursive acronym is; that won’t be on the quiz. Just be warned that PHP’s recursive acronym won’t be the last weird and slightly funny thing you’ll run across in the PHP language.
PHP Is All About the Web
If you came here for web programming, you’re in the right place. While you can write PHP programs that run from a command line (check out Figure 1 for an example), that’s not really where PHP excels.
Figure 1. Sure, you can run PHP programs from a Terminal window or a command shell on Windows. But most of the time, you won’t. PHP is perfectly suited to the Web, and that where you’ll spend most of your time.
PHP comes ready to work with HTML forms and web sessions and browser cookies. It’s great at integrating with your website’s existing authentication system, or letting you create one of your own. You’ll spend a lot of time not just handing off control to an HTML page, but actually writing the HTML you’re already familiar with right into your PHP. Lots of times, you’ll actually write some PHP, and then write some HTML, all in the same PHP file, as in the following example:
// Get the user ID of the user to show
$user_id = $_REQUEST['user_id'];
// Build the SELECT statement
$select_query = "SELECT * FROM users WHERE user_id = " . $user_id;
// Run the query
$result = mysql_query($select_query);
// Assign values to variables
<!-- All your HTML and inline PHP -->
The result? Pages that are both full of HTML and have dynamic content, like Figure 2.
PHP Is Interpreted
PHP code comes in the form of scripts, which are plain text files you write. The PHP interpreter is a piece of software on your web server that reads that file and makes sense of it, giving the Web server HTML output and directions about where to go next, or how to interpret a user’s form entry. Your text file is interpreted, one line at a time, every time that file is accessed.
This scheme is different from languages like Java or C++, which are compiled. In those languages, you write in text files, but then run a command that turns those text files into something else: class files, binary files, pieces of unreadable code that your computer uses.
What Is MySQL?
MySQL is a database. It stores your information, your users’ information, and anything else you want to stuff into it. There’s actually a lot more nuance to MySQL—and SQL, the language in which you’ll interact with MySQL (but better to save that for Chapter 3—when you’ve got a little PHP and context under your belt).
For now, think of MySQL as a warehouse where you can store things to be looked up later. Not only that, MySQL provides you a really fast little imp that runs around finding all that stuff you stuck in the warehouse whenever it’s needed. By the time you’re through this this book, you’ll love that imp…er…MySQL. It’ll do work that you could never do on your own, and it’ll do that work tirelessly and quickly.
About This Book
PHP is a web-based language, not a program that comes in a box. And there are literally tens (hundreds?) of thousands of websites that have bits of PHP instruction on them. That’s great, right? Well, not so much. Those websites aren’t all current. Some are full of bugs. Some have more information in the comment trails—scattered amongst gripes, complaints, and lambasting from other programmers—as they do in the main page. It’s no easy matter to find what you’re looking for.
The purpose of this book, then, is to serve as the manual that should have been included when you download PHP. It’s the missing PDF, if you will (or maybe the missing eBook, if you’re a Kindle or Nook or iPad person). In this book’s pages, you’ll find step-by-step instructions for getting PHP running, writing your first program…and your second program…and eventually building a web application from scratch. In addition, you’ll find clear evaluations of the absolutely critical parts of PHP that you’ll use every day, whether you’re building a personal weblog or a corporate intranet.
This book periodically recommends other books, covering topics that are too specialized or tangential for a manual about PHP and MySQL. Careful readers may notice that not every one of these titles is published by Missing Manual parent company O’Reilly Media. If there’s a great book out there that doesn’t happen to be published by O’Reilly, this book will still let you know about it.
Macintosh and Windows
PHP and MySQL work almost precisely the same in their Macintosh and Windows versions. And even more importantly, you’ll do most of your work by uploading your scripts and running your database code against a web server. That means that your hosting provider gets to deal with operating system issues. You get to focus on your code and information.
In the first few chapters, you’ll get your system set up to code and deal with PHP scripts. But you’ll soon forget about whether you’re on Mac or Windows. You’ll just be writing code, the same way you write HTML and CSS.
FTP: It’s Critical
One piece of software you won’t forget you’re using is a good FTP program. Most PHP programmers don’t sit on a remote server typing into a command-line editor like vi or emacs.
AUTHOR’S NOTE Typing in a command-line editor is actually exactly how I work. But then, I’m a dinosaur, a throwback to days when you had to watch commercials to see primetime TV, and you’d miss emails because your pocket didn’t buzz every time your boss whisked you a command through the ether.
Today, for most of you, a good text editor and a good graphical FTP client are much better choices. Seriously, my addiction owns me, and I so badly want to :wq! it.
Chapter 1 will point you at several great editors, and the fancier ones will have FTP built right in. But a program like Cyberduck (www.cyberduck.ch) is great, too. You can write a script, throw it online, and test it all with a few mouse clicks. So go ahead and get that FTP program downloaded, configured for your web server, and fired up. You’re gonna need it.
About the Outline
PHP & MySQL: The Missing Manual is divided into four parts, each containing several chapters:
§ Part 1: PHP and MySQL Basics. In the first three chapters, you’ll install PHP, get it running on your computer, write your first few PHP programs, and learn to do a few basic things like collect user information via a web form and work with text. You’ll also install MySQL and get thoroughly acquainted with the structure of a database.
§ Part 2: Dynamic Web Pages. These are the chapters where you start to build the basics of a solid web application. You’ll add a table in which you can store users and their information, and get a grasp of how easily you can manipulate text. From URLs and emails to Twitter handles, you’ll use regular expressions and string handling to bend letters, numbers, and slashes to your will.
§ Part 3: From Web Pages to Web Applications. With a solid foundation, you’re ready to connect your web pages into a more cohesive unit. You’ll add custom error handling so that your users won’t get confused when things go wrong, and your own debugging to help you find problems. You’ll also store references to users’ images of themselves, store the images themselves in a database, and learn which approach is best in which situations.
§ Part 4: Security and the Real World. In even the simplest of applications, logging in and logging out is critical. You’ll build an authentication system, and then deal with passwords (which are important, but a bit of a pain). You’ll then work with cookies and sessions, and use both to create a group-based authorization system for your web application.
At the Missing Manual website (www.missingmanuals.com/cds/phpmysqlmm), you’ll find every single code example, from every chapter, in the state it was shown for that chapter.