Introduction

Today’s web users expect exciting pages that are updated frequently and provide a customized experience. For them, web sites are more like communities, to which they’ll return time and again. At the same time, site administrators want pages that are easier to update and maintain, understanding that’s the only reasonable way to keep up with visitors’ expectations. For these reasons and more, PHP and MySQL have become the de facto standards for creating dynamic, database-driven web sites.

This book represents the culmination of my many years of web development experience coupled with the value of having written several previous books on the technologies discussed herein. The focus of this book is on covering the most important knowledge in the most efficient manner. It will teach you how to begin developing dynamic web sites and give you plenty of example code to get you started. All you need to provide is an eagerness to learn.

Well, that and a computer.

What Are Dynamic Web Sites?

Dynamic web sites are flexible and potent creatures, more accurately described as applications than merely sites. Dynamic web sites

Images Respond to different parameters (for example, the time of day or the version of the visitor’s browser)

Images Have a “memory,” allowing for user registration and login, e-commerce, and similar processes

Images Almost always integrate HTML forms, allowing visitors to perform searches, provide feedback, and so forth

Images Often have interfaces where administrators can manage the content

Images Are easier to maintain, upgrade, and build upon than statically made sites

Many technologies are available for creating dynamic web sites. The most common are ASP.NET (Active Server Pages, a Microsoft construct), JSP (JavaServer Pages), ColdFusion, Ruby on Rails (a web development framework for the Ruby programming language), and PHP. Dynamic sites don’t always rely on a database, but more and more of them do, particularly as excellent database applications like MySQL and MongoDB are available at little to no cost.

What is PHP?

PHP originally stood for “Personal Home Page” when it was created in 1994 by Rasmus Lerdorf to track the visitors to his online résumé. As its usefulness and capabilities grew (and as it started being used in more professional situations), it came to mean “PHP: Hypertext Preprocessor.”

According to the official PHP web site, found at www.php.net Letter A in a circle., PHP is a “popular general-purpose scripting language that is especially suited to web development.” It’s a long but descriptive definition, whose meaning I’ll explain.

Images

Letter A in a circle. The home page for PHP.

Starting at the end of that statement, to say that PHP is especially suited to web development means that although you can use PHP for non-web development purposes, it’s best suited for that. The corollary is that although many other technologies can be used for web development, that may not be what they’re best suited for. Simply put, if you’re hoping to do web development, PHP is an excellent choice.

Also, PHP is a scripting language, as opposed to a compiled language: PHP was designed to write web scripts, not stand-alone applications (although, with some extra effort, you can create applications in PHP). PHP scripts run only after an event occurs—for example, when a user submits a form or goes to a URL (uniform resource locator, the technical term for a web site address).

I should add to this definition that PHP is a server-side, cross-platform technology, both descriptions being important. Server-side refers to the fact that everything PHP does occurs on the server. A web server application, like Apache or Microsoft’s IIS (Internet Information Services), is required and all PHP scripts must be accessed through a URL (http://something). Its cross-platform nature means that PHP runs on most operating systems, including Windows, Unix (and its many variants), and Macintosh. More important, the PHP scripts written on one server will normally work on another with little or no modification.

At the time this book was written, PHP was at version 7.1.7. Although PHP 7 is a major release, the most important changes are in its core, with PHP 7 being significantly more performant than PHP 5.

For the most part, the examples in this book will work fine so long as you’re using at least version 5.4. Some functions and features covered will require more specific or current versions, like PHP 5.6 or greater. In those cases, I will make it clear when the functionality was added to PHP, and provide alternative solutions if you have a slightly older version of the language.

More information about PHP can always be found at PHP.net.

Why use PHP?

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 programmers to do everything they require.

Finally, the proof is in the pudding: PHP has seen an exponential growth in use since its inception, and is the server-side technology of choice on over 82 percent of all web sites Letter B in a circle.. In terms of all programming languages, PHP is the sixth most popular Letter C in a circle..

Images

Letter B in a circle. The Web Technology Surveys site provides this graphic regarding server-side technologies (www.w3techs.com/technologies/overview/programming_language/all).

A table with six columns and six rows is shown.

Letter C in a circle. The Tiobe Index (https://www.tiobe.com/tiobe-index/) uses a combination of factors to rank the popularity of programming languages.

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 I have with PHP, I’ve also developed sites using JavaServer Pages (JSP), Ruby on Rails (RoR), Sinatra (another Ruby web framework), and ASP.NET. Each has its pluses and minuses, but PHP is the technology I always return to. You might hear that it doesn’t perform or scale as well as other technologies, but Yahoo, Wikipedia, and Facebook all use PHP, and you can’t find many sites more visited or demanding than those.

You might have heard that PHP is less secure. 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.

How PHP works

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 requesting visitors (you, the client, with your browser).

When a visitor goes to a site written in PHP, the server reads the PHP code and then processes it according to its scripted directions. In the example shown in Letter D in a circle., the PHP code tells the server to send the appropriate data—HTML code—to the browser, which treats the received code as it would a standard HTML page.

A client/server model is shown.

Letter D in a circle. How PHP fits into the client/server model when a user requests a page.

This differs from a static HTML site where, when a request is made, the server merely sends the HTML data to the browser and there is no server-side interpretation occurring Letter E in a circle.. Because no server-side action is required, you can run HTML pages in your browser without using a server at all.

A client/server model is shown.

Letter E in a circle. The client/server process when a request for a static HTML page is made.

To the end user and the 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.

What is MySQL?

MySQL (www.mysql.com) Letter F in a circle. is the world’s most popular open source database. In fact, today MySQL is a viable competitor to pricey goliaths such as Oracle and Microsoft’s SQL Server (and, ironically, MySQL is owned by Oracle). Like PHP, MySQL offers excellent performance, portability, and reliability, with a moderate learning curve and little to no cost.

Screenshot of MySQL database application’s home page is shown.

Letter F in a circle. The home page for the MySQL database application.

MySQL is a database management system (DBMS) for relational databases (therefore, MySQL is an RDBMS). A database, in the simplest terms, is a collection of data, be it text, numbers, or binary files, stored and kept organized by the DBMS.

There are many types of databases, from the simple flat-file to relational to object-oriented to NoSQL. A relational database uses multiple tables to store information in its most discernible parts. Although relational databases may involve more thought in the design and programming stages, they offer improved reliability and data integrity that more than make up for the extra effort required. Further, relational databases are more searchable and allow for concurrent users.

By incorporating a database into a web application, some of the data generated by PHP can be retrieved from MySQL Letter G in a circle.. This further moves the site’s content from a static (hard-coded) basis to a flexible one, flexibility being the key to a dynamic web site.

Dynamic applications using both PHP and MySQL is depicted.

Letter G in a circle. How most of the dynamic applications in this book will work, using both PHP and MySQL.

MySQL is an open source application, like PHP, meaning that it is free to use or even modify (the source code itself is downloadable). There are occasions when you should pay for a MySQL license, especially if you are making money from the sales or incorporation of the MySQL product. Check MySQL’s licensing policy for more information on this.

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 utilities for maintenance and other purposes. PHP has always had good support for MySQL, and that is even truer in the most recent versions of the language.

MySQL has been known to handle databases as large as 60,000 tables with more than several billion rows. MySQL can work with tables as large as thousands of terabytes on some operating systems, generally a healthy 4 GB otherwise. MySQL is used by NASA and the U.S. Census Bureau, among many others.

As of this writing, MySQL is on version 5.7.18. The version of MySQL you have affects what features you can use, so it’s important that you know what you’re working with. For this book, MySQL 5.7.14 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 5.0.

What You’ll Need

To follow the examples in this book, you’ll need the following tools:

Images A web server application (for example, Apache, Nginx, or IIS)

Images PHP

Images MySQL

Images A browser (Microsoft’s Internet Explorer or Edge, Mozilla’s Firefox, Apple’s Safari, Google’s Chrome, etc.)

Images A text editor, PHP-capable WYSIWYG application (Adobe’s Dreamweaver qualifies), or IDE (integrated development environment)

Images 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, browsers can be had without cost, and many good text editors are available for nothing.

The appendix discusses the installation process on the Windows and macOS 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 Letter D in a circle. and Letter E in a circle.). Conversely, you could purchase web hosting for only dollars per month that will provide you with a PHP- and MySQL-enabled environment already online.

About This Book

This book teaches you how to develop dynamic web sites with PHP and MySQL, covering the knowledge that most developers might require. In keeping with the format of the Visual QuickPro series, the information is discussed using a step-by-step approach with corresponding images. The focus has been kept on real-world, practical examples, avoiding “here’s something you could do but never would” scenarios. As a practicing web developer myself, I wrote about the information that I use and avoided those topics immaterial to the task at hand. As a practicing writer, I made certain to include topics and techniques that I know readers are asking about.

The structure of the book is linear, and the intention is that you’ll read it in order. It begins with three chapters covering the fundamentals of PHP (by the second chapter, you will have already developed your first dynamic web page). After that, there are four chapters on SQL (Structured Query Language, which is used to interact with all databases) and MySQL. Those chapters teach the basics of SQL, database design, and the MySQL application in particular. Then there’s one chapter on debugging and error management, information everyone needs. This is followed by a chapter introducing how to use PHP and MySQL together, a remarkably easy thing to do.

The following five chapters teach more application techniques to round out your knowledge. Security, in particular, is repeatedly addressed in those pages. The next two chapters expand your newfound knowledge into subjects that, though not critical, are ones you’ll want to pick up in time regardless. Finally, I’ve included two example chapters, in which the heart of different web applications are developed, with instructions.

Is this book for you?

This book was written for a wide range of people within the beginner-to-intermediate range. The book makes use of HTML5, so solid experience with HTML is a must. Although this book covers many things, it does not formally teach HTML or web design. Some CSS is sprinkled about these pages but also not taught.

Second, this book expects that you have one of the following:

Images The drive and ability to learn without much hand holding, or…

Images Familiarity with another programming language (even solid JavaScript skills would qualify), or…

Images A cursory knowledge of PHP

Make no mistake: This book covers PHP and MySQL from A to Z, teaching everything you’ll need to know to develop real-world web sites, but the early chapters in particular cover PHP at a quick pace. For this reason I recommend either some programming experience or a curious and independent spirit when it comes to learning new things. If you find that the material goes too quickly, you should probably start off with the latest edition of my book PHP for the World Wide Web: Visual QuickStart Guide, which goes at a much more tempered pace.

No database experience is required, since SQL and MySQL are discussed starting at a more basic level.

What’s new in this edition

The first four editions of this book have been very popular, and I’ve received a lot of positive feedback on them (thanks!). In writing this new edition, I focused on ensuring the material is accurate, up to date, and in keeping with today’s standards and best practices. The changes in this edition include

Images Updating all the code to use HTML5

Images Use of more modern HTML design techniques, including multiple examples of the Twitter Bootstrap framework

Images Updating everything for the latest versions of PHP and MySQL

Images Additional PHP and MySQL examples, such as performing transactions from a PHP script

Images Even more information and examples for improving the security of your scripts and sites

Images Removal of outdated content (e.g., things used in older versions of PHP or no longer applicable)

Images Return of the installation appendix to the printed book (in the fourth edition, the appendix was freely available online instead)

For those of you that also own a previous edition (thanks, thanks, thanks!), I hope you find this to be a fresh and sharp update to an already excellent resource.

How this book compares to my other books

This is my fourth PHP and/or MySQL title, after (in order)

Images PHP for the World Wide Web: Visual QuickStart Guide

Images PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide

Images 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 beginner’s guide to PHP. This title overlaps it some, mostly in the first three chapters, but uses new examples so as not to be redundant. 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 on many things taught here. The MySQL book focuses 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 experience 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 PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide. If you are most interested in MySQL and are not concerned with learning much about PHP, check out MySQL: Visual QuickStart Guide.

That being said, if you want to learn everything you need to know to begin developing dynamic web sites with PHP and MySQL today, then this is the book for you! It references 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.

Companion Web Site

I have developed a companion web site specifically for this book, which you may reach at LarryUllman.com. 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 problems 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 a popular forum where readers can ask and answer each other’s questions (I answer many of them myself), and more!

Questions, comments, or suggestions?

If you have any questions on PHP or MySQL, you can turn to one of the many web sites, mailing lists, newsgroups, and FAQ repositories 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 online 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 information is available on my site. I do try to answer every email I receive, although I cannot guarantee a quick reply.

Accessing the free Web Edition

Your purchase of this book in any format includes access to the corresponding Web Edition, which provides several special online-only features:

Images The complete text of the book, with all the figures and in full color

Images Updates and corrections as they become available

The Web Edition can be viewed on all types of computers and mobile devices with any modern web browser that supports HTML5. To get access to the Web Edition of PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide all you need to do is register this book:

1. Go to www.peachpit.com/register.

2. Sign in or create a new account.

3. Enter ISBN: 9780134301846.

4. Answer the questions as proof of purchase.

The Web Edition will appear under the Digital Purchases tab on your Account page. Click the Launch link to access the product.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.116.143