. November 2011: First Edition. November 2012: Second Edition. Revision History for the Second Edition: 2012-11-5 First release See http://oreilly.com/catalog/errata.csp?isbn=0636920024927 for release details. The Missing Manual is a registered trademark of O’Reilly Media, Inc. The Missing Manual logo, and “The book that should have been in the box” 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 is aware of a trademark claim, the designations are capitalized. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained in it. [LSI] ISBN: 978-1-449-32557-2 III Contents The Missing Credits vii Introduction 1 Part One: PHP and MySQL Basics CHAPTER 1: PHP: What, Why, and Where? 15 PHP Comes in Two Flavors: Local and Remote. . . . . . . . . . . . . . . . . . . . . . . . . 15 PHP: Going Local 21 Write Your First Program 38 Run Your First Program 40 But Where’s That Web Server? 42 CHAPTER2: PHP Meets HTML 45 Script or HTML? 46 PHP Talks Back 51 Run PHP Scripts Remotely 54 CHAPTER 3: PHP Syntax: Weird and Wonderful 61 Get Information from a Web Form 62
VII The Missing Credits ABOUT THE AUTHOR Brett McLaughlin is a senior-level technologist and strategist, active especially in web programming and data-driven, custom - er-facing systems. Rarely focused on only one component of a system, he architects, designs, manages, and implements large- scale applications from start to ﬁnish with mission-critical imple - mentations and deadlines. Of course, that’s all fancy-talk for saying that Brett’s a geek, spending most of his day in front of a computer with his hands ﬂying across a keyboard. Currently, he spends most of his time working on NASA projects, which sounds much cooler than it actually is. But hey, maybe that satellite overhead really is controlled by PHP and MySQL ABOUT THE CREATIVE TEAM Nan Barber (editor) has been working on the Missing Manual series since its incep- tion. She lives in Boston with her husband and various electronic devices. Email: firstname.lastname@example.org . Holly Bauer (production editor) lives in Ye Olde Cambridge, Massachusetts, where she is an avid home cook, proliﬁc DIYer, and mid-century modern furniture design enthusiast. Email: email@example.com . Bob Russell (copyeditor) is a documentation specialist and President of Octal Pub - lishing, Inc., in Salem, New Hampshire ( www.octalpub.com ). Email: bob.russell@ octalpub.com . Bob Pfahler (indexer) is a freelance indexer. For the past ﬁve years, he has indexed many computer books as well as biographies, history, and business books. When he is not working, he likes to take bike rides in the foothills outside of Denver. He in - dexed this book as an associate for Potomac Indexing ( www.potomacindexing.com ). Roger House (technical reviewer) is a freelance software developer living in northern California. He has written code in many languages for various kinds of applications. He enjoys algorithm design, use of data structures, and applications of mathematics. Web: www.rogerfhouse.com . Email: firstname.lastname@example.org . Steve Suehring (technical reviewer) is a technical architect with an extensive back - ground ﬁnding simple solutions to complex problems. Steve plays several musical instruments (not at the same time) and can be reached through his website www . braingia.org . THE MISSING CREDITS VIII ACKNOWLEDGMENTS Acknowledgments are nearly impossible to do well. Before you can thank anyone of substance, the music swells and they’re shuing you o stage. Seriously, apart from the writing, there’s my wife, Leigh, and my kids, Dean, Robbie, and Addie. Any energy or joy or relaxation that happens during the long writing process ﬁlters through those four, and there are never enough royalties to cover the time lost with them. I suppose it’s a reﬂection of their love and support for me that they’re OK with me writing anyway. There’s certainly the writing. Brian Sawyer was the ﬁrst guy to call me when I became available to write, and he called when I was really in need of just what he gave me: excitement about me writing and encouragement that I could write for the Missing Manual series. I won’t forget that call anytime soon. And, there’s Nan Barber, who IM’ed and emailed me throughout the entire process. She showed a really unhealthy level of trust that wasn’t earned, and I’m quite thankful especially in the dark days of early August, when I had hundreds of pages left to write, in just a few short weeks. Roger House and Steve Suehring, my technical reviewers, were both picky and gentle. That’s about all you can ask. And Steve ﬁlled out my PHP holes. He caught one particularly nasty issue that I think vastly improved the book. You don’t realize this, but you owe him a real debt of thanks if this book helps you. — Brett McLaughlin THE MISSING MANUAL SERIES Missing Manuals are witty, superbly written guides to computer products that don’t come with printed manuals (which is just about all of them). Each book features a handcrafted index and cross-references to speciﬁc pages (not just chapters). Recent and upcoming titles include: Access 2010: The Missing Manual by Matthew MacDonald Adobe Edge Animate: The Missing Manual by Chris Grover Buying a Home: The Missing Manual by Nancy Conner CSS3: The Missing Manual, Third Edition, by David Sawyer McFarland Creating a Website: The Missing Manual, Third Edition, by Matthew MacDonald David Pogue’s Digital Photography: The Missing Manual by David Pogue Dreamweaver CS5.5: The Missing Manual by David Sawyer McFarland Droid 2: The Missing Manual by Preston Gralla Droid X2: The Missing Manual by Preston Gralla Excel 2010: The Missing Manual by Matthew MacDonald Facebook: The Missing Manual, Third Edition by E.A. Vander Veer THE MISSING CREDITS IX FileMaker Pro 12: The Missing Manual by Susan Prosser and Stuart Gripman Flash CS5.5: The Missing Manual by Chris Grover Galaxy S II: The Missing Manual by Preston Gralla Galaxy Tab: The Missing Manual by Preston Gralla Google Apps: The Missing Manual by Nancy Conner Google SketchUp: The Missing Manual by Chris Grover HTML5: The Missing Manual by Matthew MacDonald iMovie ’11 & iDVD: The Missing Manual by David Pogue and Aaron Miller iPad: The Missing Manual,
// Build the SELECT statement $select_query = "SELECT * FROM users WHERE user_id = " . $user_id; PHP & MYSQL: THE MISSING MANUAL 4 WHAT IS PHP? // Run the query $result = mysql_query($select_query);
// Assign values to variables ?>
into your browser to hop directly to the Registration page. Feedback Got questions? Need more information? Fancy yourself a book reviewer? On the Feedback page, you can get expert answers to questions that come to you while reading, share your thoughts on this Missing Manual, and ﬁnd groups for folks who share your interest in PHP, MySQL, and web applications in general. To have your say, go to www.missingmanuals.com/feedback . Errata In an eort to keep this book as up-to-date and accurate as possible, each time we print more copies, we’ll make any conﬁrmed corrections you’ve suggested. We also note such changes on the book’s website, so you can mark important corrections into your own copy of the book, if you like. Go to http://tinyurl.com/phpmysql2e-mm
to report an error and view existing corrections. INTRODUCTION 11 SAFARI® BOOKS ONLINE Safari® Books Online Safari® Books Online is an on-demand digital library that lets you easily search over 24,000 technology and creative reference books and videos to ﬁnd the answers you need quickly. With a subscription, you can read any page and watch any video from the library online. You can read books on your cell phone and mobile devices; access new titles before they are available for print; and get exclusive access to manuscripts in devel - opment and post feedback for the authors. You can copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and beneﬁt from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com .