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.