




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































PHP 6
AND MY SQL 5
Dedication
Dedicated to the fine faculty at my alma mater, Northeast Missouri State University. In particular, I would like to thank: Dr. Monica Barron, Dr. Dennis Leavens, Dr. Ed Tyler, and Dr. Cole Woodcox, whom I also have the pleasure of calling my friend. I would not be who I am as a writer, as a student, as a teacher, or as a person if it were not for the magnanimous, affecting, and brilliant instruction I received from these educators.
Special Thanks to:
My heartfelt thanks to everyone at Peachpit Press, as always.
My gratitude to editor extraordinaire Rebecca Gulick, who makes my job so much easier. And thanks to Bob Campbell for his hard work, helpful suggestions, and impressive attention to detail. Thanks also to Rebecca Plunkett for indexing and Becky Winter, Myrna Vladic, Jerry Ballew, and Rick Gordon for laying out the book, and thanks to Arpad Ray for his technical review.
Kudos to the good people working on PHP, MySQL, Apache, phpMyAdmin, and XAMPP, among other great projects. And a hearty “cheers” to the denizens of the various news- groups, mailing lists, support forums, etc., who offer assistance and advice to those in need.
Thanks, as always, to the readers, whose sup- port gives my job relevance. An extra helping of thanks to those who provided the transla- tions in Chapter 15, “Example—Message Board,” and who offered up recommendations as to what they’d like to see in this edition.
Thanks to Nicole and Christina for enter- taining and taking care of the kids so that I could get some work done.
Finally, I would not be able to get through a single book if it weren’t for the love and support of my wife, Jessica. And a special shout out to Zoe and Sam, who give me rea- sons to, and not to, write books!
Table of Contents
Table of Contents
Introduction
What Are Dynamic Web Sites? Dynamic Web sites are flexible and potent creatures, more accurately described as applications than merely sites. Dynamic Web sites ◆ (^) Respond to different parameters (for example, the time of day or the version of the visitor’s Web browser) ◆ (^) Have a “memory,” allowing for user regis- tration and login, e-commerce, and simi- lar processes ◆ (^) Almost always have HTML forms, so that people can perform searches, provide feedback, and so forth
◆ (^) Often have interfaces where administra- tors can manage the site’s content ◆ (^) Are easier to maintain, upgrade, and build upon than statically made sites There are many technologies available for creating dynamic Web sites. The most com- mon are ASP.NET (Active Server Pages, a Microsoft construct), JSP (Java Server Pages), ColdFusion, Ruby on Rails, and PHP. Dynamic Web sites don’t always rely on a database, but more and more of them do, particularly as excellent database applications like MySQL are available at little to no cost.
Introduction
What Are Dynamic Web Sites?
Figure i.1 The home page for PHP.
Because of the planned extinction of PHP 4, many users and Web hosting companies will likely make a quick transition from PHP 4 to PHP 5 to PHP 6. To discuss what’s new in PHP 6, I’ll start with the even bigger differ- ences between PHP 4 and 5. PHP 5, like PHP 4 before it, is a major new development of this popular programming language. The most critical changes in PHP 5 involve object-oriented programming (OOP).Those changes don’t really impact this book, as OOP isn’t covered (I do so in my book PHP 5 Advanced: Visual QuickPro Guide). With respect to this book, the biggest change in PHP 5 is the addition of the Improved MySQL Extension, which is used to communicate with MySQL. The Improved MySQL Extension offers many benefits over the older MySQL extension and will be used exclusively. The big change in PHP 6 is support for Unicode, which is to say that PHP can now handle characters in every language in the world. This is huge, and it’s also one of the reasons it’s taken a while to release PHP 6. What this means in terms of programming is covered in Chapter 14, “Making Universal Sites.” The information in that chapter is also used in Chapter 15, “Example—Message Board.” Beyond Unicode support, PHP 6 cleans up a lot of garbage that was left in PHP 5 even though the recommendation was not to use such things. The two biggest removals are the “Magic Quotes” and “register globals” features.
Put simply, when it comes to developing dynamic Web sites, PHP is better, faster, and easier to learn than the alternatives. What you get with PHP is excellent performance, a tight integration with nearly every database available, stability, portability, and a nearly limitless feature set due to its extendibility. All of this comes at no cost (PHP is open source) and with a very manageable learning curve. PHP is one of the best marriages I’ve ever seen between the ease with which beginning programmers can start using it and the ability for more advanced program- mers to do everything they require. Finally, the proof is in the pudding: PHP has seen an exponential growth in use since its inception, overtaking ASP as the most pop- ular scripting language being used today. It’s the most requested module for Apache (the most-used Web server), and by the time this book hits the shelves, PHP will be on nearly 25 million domains. Of course, you might assume that I, as the author of a book on PHP (several, actually), have a biased opinion. Although not nearly to the same extent as PHP, I’ve also devel- oped sites using Java Server Pages (JSP), Ruby on Rails (RoR), and ASP.NET. Each has its pluses and minuses, but PHP is the tech- nology I always return to. You might hear that it doesn’t perform or scale as well as other technologies, but Yahoo! handles over 3.5 billion hits per day using PHP (yes, billion). You might also wonder how secure PHP is. But security isn’t in the language; it’s in how that language is used. Rest assured that a complete and up-to-date discussion of all the relevant security concerns is provided by this book!
Introduction
What Are Dynamic Web Sites?
As previously stated, PHP is a server-side language. This means that the code you write in PHP sits on a host computer called a server. The server sends Web pages to the request- ing visitors (you, the client, with your Web browser).
When a visitor goes to a Web site written in PHP, the server reads the PHP code and then processes it according to its scripted direc- tions. In the example shown in Figure i.2, the PHP code tells the server to send the appropriate data—HTML code—to the Web browser, which treats the received code as it would a standard HTML page.
This differs from a static HTML site where, when a request is made, the server merely sends the HTML data to the Web browser and there is no server-side interpretation occurring (Figure i.3). Because no server- side action is required, you can run HTML pages in your Web browser without using a server at all. To the end user and their Web browser there is no perceptible difference between what home.html and home.php may look like, but how that page’s content was created will be significantly different.
Introduction
What Are Dynamic Web Sites?
URL Request HTML
Client Server
PHP
HTML
Script Request
Figure i.2 How PHP fits into the client/server model when a user requests a Web page.
URL Request
HTML
Client (^) Server
Figure i.3 The client/server process when a request for a static HTML page is made.
otherwise. MySQL is used by NASA and the United States Census Bureau, among many others. At the time of this writing, MySQL is on ver- sion 5.0.45, with versions 5.1 and 6.0 in devel- opment. The version of MySQL you have affects what features you can use, so it’s important that you know what you’re work- ing with. For this book, MySQL 5.0.45 was used, although you should be able to do everything in this book as long as you’re using a version of MySQL greater than 4.1. (My book MySQL: Visual QuickStart Guide goes into the more advanced and newer features of MySQL 5 that aren’t used in this book.)
Introduction
What Are Dynamic Web Sites?
Pronunciation Guide Trivial as it may be, I should clarify up front that MySQL is technically pronounced “My Ess Que Ell,” just as SQL should be said “Ess Que Ell.” This is a question many people have when first working with these technologies. While not a critical issue, it’s always best to pronounce acronyms correctly.
The MySQL software consists of several pieces, including the MySQL server (mysqld, which runs and manages the databases), the MySQL client (mysql, which gives you an interface to the server), and numerous utili- ties for maintenance and other purposes. PHP has always had good support for MySQL, and that is even more true in the most recent versions of the language.
MySQL has been known to handle databases as large as 60,000 tables with more than five billion rows. MySQL can work with tables as large as eight million terabytes on some operating systems, generally a healthy 4 GB
URL Request
HTML
Client Server
PHP MySQL
HTML
Script Request Query Data
Figure i.5 How most of the dynamic Web applications in this book will work, using both PHP and MySQL.
What You’ll Need To follow the examples in this book, you’ll need the following tools: ◆ (^) A Web server application (for example, Apache, Abyss, or IIS) ◆ (^) PHP ◆ (^) MySQL ◆ (^) A Web browser (Microsoft’s Internet Explorer, Mozilla’s Firefox, Apple’s Safari, etc.) ◆ (^) A text editor, PHP-capable WYSIWYG application (Adobe’s Dreamweaver quali- fies), or IDE (integrated development environment) ◆ (^) An FTP application, if using a remote server One of the great things about developing dynamic Web sites with PHP and MySQL is that all of the requirements can be met at no cost whatsoever, regardless of your operating system! Apache, PHP, and MySQL are each free; most Web browsers can be had without cost; and many good text editors are avail- able for nothing. The appendix discusses the installation process on the Windows and Mac OS X operating systems. If you have a computer, you are only a couple of downloads away from being able to create dynamic Web sites (in that case, your computer would represent both the client and the server in Figures i. and i.5). Conversely, you could purchase Web hosting for only dollars per month that will provide you with a PHP- and MySQL-enabled environment already online.
Introduction
What You'll Need
The first two editions of this book have been very popular, and I’ve received a lot of posi- tive feedback on them (thanks!). In writing this new edition, I wanted to do more than just update the material for the latest ver- sions of PHP and MySQL, although that is an overriding consideration throughout the book. Other new features you’ll find are: ◆ (^) New examples demonstrating techniques frequently requested by readers ◆ (^) Some additional advanced MySQL and SQL examples ◆ (^) A dedicated chapter on thwarting com- mon Web site abuses and attacks ◆ (^) A brand-new chapter on working with multiple languages and time zones ◆ (^) A brand-new example chapter on creat- ing a message board (or forum) ◆ (^) Expanded and updated installation and configuration instructions ◆ (^) Removal of outdated content (e.g., things used in older versions of PHP or not applicable to PHP 6) For those of you that also own the first and/or second edition (thanks, thanks, thanks!), I believe that these new features will also make this edition a required fixture on your desk or bookshelf.
This is my fourth PHP and/or MySQL title, after (in order) ◆ (^) PHP for the World Wide Web: Visual QuickStart Guide ◆ (^) PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide ◆ (^) MySQL: Visual QuickStart Guide
I hope this résumé implies a certain level of qualification to write this book, but how do you, as a reader standing in a bookstore, decide which title is for you? Of course, you are more than welcome to splurge and buy the whole set, earning my eternal gratitude, but… The PHP for the World Wide Web: Visual QuickStart Guide book is very much a begin- ner’s guide to PHP. This title overlaps it some, mostly in the first three chapters, but uses new examples so as not to be redun- dant. For novices, this book acts as a follow- up to that one. The advanced book is really a sequel to this one, as it assumes a fair amount of knowledge and builds upon many things taught here. The MySQL book focus- es almost exclusively on MySQL (there are but two chapters that use PHP). With that in mind, read the section “Is this book for you?” and see if the requirements apply. If you have no programming experi- ence at all and would prefer to be taught PHP more gingerly, my first book would be better. If you are already very comfortable with PHP and want to learn more of its advanced capabilities, pick up the second. If you are most interested in MySQL and are not concerned with learning much about PHP, check out the third. That being said, if you want to learn every- thing you need to know to begin developing dynamic Web sites with PHP and MySQL today, then this is the book for you! It refer- ences the most current versions of both technologies, uses techniques not previously discussed in other books, and contains its own unique examples. And whatever book you do choose, make sure you’re getting the most recent edition or, barring that, the edition that best matches the versions of the technologies you’ll be using.
Introduction
About This Book
Companion Web Site
I have developed a companion Web site specifically for this book, which you may reach at www.DMCinsights.com/phpmysql3/ (Figure i.6). There you will find every script from this book, a text file containing lengthy SQL commands, and a list of errata that occurred during publication. (If you have problem with a command or script, and you are following the book exactly, check the errata to ensure there is not a printing error before driving yourself absolutely mad.) At this Web site you will also find useful Web links, a highly popular forum where readers can ask and answer each other’s questions (I answer many of them myself), and more!
If you have any questions on PHP or MySQL, you can turn to one of the many Web sites, mailing lists, newsgroups, and FAQ reposito- ries already in existence. A quick search online will turn up virtually unlimited resources. For that matter, if you need an immediate answer, those sources or a quick Web search will most assuredly serve your needs (in all likelihood, someone else has already seen and solved your exact problem). You can also direct your questions, comments, and suggestions to me. You’ll get the fastest reply using the book’s corresponding forum (I always answer those questions first). If you’d rather email me, my contact informa- tion is available on the Web site. I do try to answer every email I receive, although I can- not guarantee a quick reply.
Introduction
Companion Web Site
Figure i.6 The companion Web site for this book.