No doubt the symbiotic nature of PHP and MySQL helped propel them both forward, but what attracted developers to them in the first place? The simple answer has to be the ease with which you can use them to quickly create dynamic elements on websites. MySQL is a fast and powerful yet easy-to-use database system that offers just about anything a website would need in order to find and serve up data to browsers. When PHP allies with MySQL to store and retrieve this data, you have the fundamental parts required for the development of social networking sites and the beginnings of Web 2.0.
With PHP, it’s a simple matter to embed dynamic activity in web pages. When you give pages the .php extension, they have instant access to the scripting language. From a developer’s point of view, all you have to do is write code such as the following:
<?php echo "Hello World. Today is ".date("l").". "; ?> How are you?
the web server to allow the PHP program to interpret all the following
code up to the
Outside of this construct, everything is sent to the client as direct
HTML. So the text “How are you?” is simply output to the browser;
within the PHP tags, the built-in
date function displays the current day of
the week according to the server’s system time.
The final output of the two parts looks like this:
Hello World. Today is Wednesday. How are you?
PHP is a flexible language, and some people prefer to place the PHP construct directly next to PHP code, like this:
Hello World. Today is <?php echo date("l"); ?>. How are you?
There are also other ways of formatting and outputting information, which I’ll explain in the chapters on PHP. The point is that with PHP, web developers have a scripting language that although not as fast as compiling your code in C or a similar language, is incredibly speedy and that also integrates seamlessly with HTML code.
If you intend to type in the PHP examples in this book to work
along with me, you must remember to add
<?php in front and
?> after them to ensure that the PHP
interpreter processes them. To facilitate this, you may wish to
prepare a file called example.php with those
tags in place.
Using PHP, you have unlimited control over your web server. Whether you need to modify HTML on the fly, process a credit card, add user details to a database, or fetch information from a third-party website, you can do it all from within the same PHP files in which the HTML itself resides.
Of course, there’s not a lot of point to being able to change HTML output dynamically unless you also have a means to track the changes that users make as they use your website. In the early days of the Web, many sites used “flat” text files to store data such as usernames and passwords. But this approach could cause problems if the file wasn’t correctly locked against corruption from multiple simultaneous accesses. Also, a flat file can get only so big before it becomes unwieldy to manage—not to mention the difficulty of trying to merge files and perform complex searches in any kind of reasonable time.
That’s where relational databases with structured querying become essential. And MySQL, being free to use and installed on vast numbers of Internet web servers, rises superbly to the occasion. It is a robust and exceptionally fast database management system that uses English-like commands.
The highest level of MySQL structure is a database, within which
you can have one or more tables that contain your data. For example,
let’s suppose you are working on a table called
users, within which you have created columns
INSERT INTO users VALUES('Smith', 'John', 'firstname.lastname@example.org');
Of course, as mentioned earlier, you will have issued other
commands to create the database and table and to set up all the
correct fields, but the
command here shows how simple it can be to add new data to a database.
INSERT command is an example of
SQL (which stands for “Structured Query Language”), a language
designed in the early 1970s and reminiscent of one of the oldest
programming languages, COBOL. It
is well suited, however, to database queries, which is why it is still
in use after all this time.
It’s equally easy to look up data. Let’s assume that you have an email address for a user and need to look up that person’s name. To do this, you could issue a MySQL query such as:
SELECT surname,firstname FROM users WHERE email@example.com';
As you’d expect, there’s quite a bit more that you can do with
MySQL than just simple
SELECT commands. For example, you
can join multiple tables according to various criteria, ask for
results in a variety of different orders, make partial matches when
you know only part of the string that you are searching for, return
only the nth result, and a lot more.
Using PHP, you can make all these calls directly to MySQL without having to run the MySQL program yourself or use its command-line interface. This means you can save the results in arrays for processing and perform multiple lookups, each dependent on the results returned from earlier ones, to drill right down to the item of data you need.
For even more power, as you’ll see later, there are additional functions built right in to MySQL that you can call up for common operations and extra speed.
Thankfully, the manufacturers have mostly now come to their senses and have realized the need for full compatibility between each other, so web developers don’t have to write multiexception code. But there remain millions of legacy browsers that will be in use for a good many years to come. Luckily, there are solutions for the incompatibility problems, and later in this book we’ll look at libraries and techniques that enable you to safely ignore these differences.
This code snippet tells the web browser to interpret everything
script tags as
World. Today is ” to the current
Date. The result will look
something like this:
Hello World. Today is Sun Jan 01 2012 14:14:00
It’s worth knowing that unless you need to specify an exact
<script> to start the
Ajax is the main process behind what is now known as Web 2.0 (a term coined by Tim O’Reilly, the founder and CEO of this book’s publishing company), in which web pages have started to resemble standalone programs, because they don’t have to be reloaded in their entirety. Instead, a quick Ajax call can pull in and update a single element on a web page, such as changing your photograph on a social networking site or replacing a button that you click with the answer to a question. This subject is fully covered in Chapter 18.