How to Do Everything with PHP and MySQL

This book review was published by Slashdot, .

How to Do Everything with PHP and MySQL

When it comes to creating a dynamic website with data store capabilities, the site developer can choose from many technologies, including PHP and MySQL. The combination of the two is rapidly emerging as a favorite, partly because they work well together, and partly because they are both available under open-source licenses. As a result, technical book publishers are cranking out a growing number of high-priced tomes that try to cover all of the details of either technology, or both. But for the developer just getting started in either one, there is a new title that could prove more accessible: How to Do Everything with PHP and MySQL, by Vikram Vaswani, and published by McGraw-Hill Osborne Media.

The publisher has a page on their website devoted to the book. It lists the publication date (28 February 2005), ISBN number (978-0072257953), a brief description, and other details. For some reason, it lists the book as containing 400 pages, and yet my copy has 381. The page also has links to a table of contents and a sample chapter, namely, the first one. For those readers with very slow Internet access or unstable Adobe Acrobat plugins installed in their Web browser, beware that the sample chapter — and even the table of contents — are offered only in PDF format; but the two links give no warning.

Most technical publishers, for every one of their books, wisely have links to the errata and sample code, right there on each book's Web page. This is the best approach, because when readers are having difficulty getting a book's examples to work correctly, they want to be able to quickly find and download the most up-to-date sample code, as well as check the errata page for any bugs in the printed code. Unfortunately, McGraw-Hill Osborne Media has their links to those two types of information in an easily-overlooked part of a menubar, using small black text on a blue background. The links are near the upper left-hand corner, and outside the content section of the Web page, where the typical reader would be seeking fruitlessly for them.

The companion website for the book is hosted by Vaswani's software consulting firm, Melonfire. The site has the book's table of contents (in HTML), a link to chapter 1 in PDF, a profile of the author, three full-length case studies, a feedback form, and an extensive collection of links to PHP and MySQL reference material, discussion lists, articles, and tutorials. At the end of the Introduction in the book, the author invites the reader to use that companion site for connecting with other PHP users, and sharing their thoughts on PHP and MySQL development. The site itself has no such forum, so the author probably meant the discussion lists.

The companion site also has a link to download a Zip file containing all of the sample applications from the book — from chapters 7, 12, and 16 — comprising nine PHP scripts, an SQL file, and a data file. The code snippets themselves do not appear to be included in the download. This shouldn't pose a difficulty for the typical reader, since few of the code snippets are long. Besides, typing them in on one's computer can help to reinforce the language syntax that one is learning, as well as decent code formatting (valuable for newbies).

The book is organized into four parts, the first of which presents the basics of PHP and MySQL, including the history and features of both technologies, as well as how to install them on Unix and Windows systems, verify the integrity of the installations, and make some critical security and configuration changes, such as changing passwords. Parts II and III cover the basics of PHP and MySQL, respectively. The fourth and final part describes how to use the two together. To that end, every chapter contains snippets of code to illustrate the ideas being described. In addition, each section is wrapped up and illustrated with a sample application. For PHP, the author shows how to build a session-based shopping cart. For MySQL, he presents a simple order tracking system. For using PHP and MySQL together, he shows a news publishing system.

Despite its misleading title, the book clearly does not tell the reader how to do everything with PHP and MySQL. As the author notes in the Introduction, the book is not designed to be a complete reference for either technology, but instead intended as a tutorial for Web developers who are interested in learning how to do server-side scripting in combination with a database management system. Vaswani states that he does not assume prior knowledge of programming or database fundamentals, and that these basic concepts will be taught by example, using tutorials and realistic examples. I suspect a reader not familiar with HTML, however, could be easily baffled by the book. On the other hand, most if not all developers reading a book on PHP or MySQL, likely already know HTML well enough to understand the output of PHP-enhanced Web pages.

One strength that this book has over many similar ones, is that the author explains upfront how to install PHP and MySQL, rather than relegating these topics to an appendix, or skipping them entirely. This is critical, because many programmers will find that the most challenging aspects of getting started with PHP and MySQL, are simply getting them installed and working, along with a Web server, such as Apache — and not coding the applications themselves.

Another welcome aspect of the book is the author's enthusiasm for the technologies — although characterizing MySQL as "quite friendly" (page 150) is a stretch. Furthermore, his explanations are clear and concise. In addition, Vaswani makes no pretense that his book has all the answers; he frequently refers the reader to URLs in the online manuals of the products, for more details. In addition, he does a nice job of illustrating the advantages of normalized database tables, and later explaining how to format query output — an important topic omitted in many similar books.

Yet, like all books, this one is not perfect. There are a number of errors or pitfalls in the book that could confuse the reader. They range from incomplete explanations of what a user will see when running particular commands, to the sort of errata one finds in all technical books. I found over two dozen in total (there may be more). They are detailed below.

The instructions for testing MySQL, Apache, and PHP (in that order) are separate from the instructions for installing them, which doesn't seem logical. After installing each one, it's best to verify that the installation worked before adding more moving parts; otherwise, if one of those three elements is misbehaving, then it could be much more difficult to discern which one is truly the source of the problem.

The chapter openings could be more concise, especially as some of the bulleted items repeat the preceding non-bulleted material. The chapter summaries provide hyperlinks and high level perspective, which are useful. But they also contain restatements of what the chapter covered; these seem like padding, and should be removed.

In the book's text, dashes are used several times, to separate phrases (which is grammatically correct). But they are as small as hyphens, making each pair of words appear hyphenated.

The code snippets are more than adequate, but the HTML code would be more readable if it were indented properly, as is the PHP code. In addition, Vaswani uses a non-standard <p />, rather than the proper <p> and </p> tags.

Software tools can change rapidly, including shortly after a book is edited and published. The book's version of MySQL is 4.0.21, and the version recommended by the creators of MySQL (as of this writing) is 4.1.12. The configuration tool in the former appears to be WinMySQLadmin, while that in the latter is the MySQL Server Instance Configuration Wizard. They look quite different from one another, so do not let that throw you. The Server Instance Configuration Wizard, which now starts after installation by default, is similar and equally capable.

On page 18, in Figure 1-1, the example address of a Webmail service is "http://my.server.com/", which probably would not be appreciated or approved by the folks at Server.com. A better Web address to use would be "http://mail.example.com/", since the domain "example.com" is intended for just that purpose (see page 72). Similarly, on page 212, the domain "goodguys.com" is used, and should be replaced with "example.com". The same holds true for "www.guess.it" on page 297.

On pages 30 and 216, the MySQL server program is listed as "mysqld_safe". But that executable does not exist within the "bin" directory in version 4.1.12. Instead, I used "mysqld-nt.exe" (on a Windows 2000 server), and that worked fine.

In the version of MySQL that I tried, the command "mysql -u root", on page 50, returned "ERROR 1045". The command might work if no password is set for the root user — I'm not sure. But the MySQL Server Instance Configuration Wizard prompts for a root password to be set beforehand. The solution is to do "mysql -u root -p" instead, to force it to ask for the password that one chose.

In the third chapter, on page 75, the author neglects to distinguish between prefix and postfix auto-incrementation and auto-decrementation. On the following page, "!" is shown as having the same operator precedence as "++" and "--", while the PHP manual gives "!" lower precedence; yet it appears inconsequential, given how the operators can be used.

On pages 110 and 112, Vaswani states that certain code can generate warnings, but does not tell the reader where those warnings can be found. Similarly, he doesn't explain where die() output will be read. Is he assuming the reader is running the PHP pages on a command line? Most readers are probably loading them in their browser. Also, on page 112, he mentions how to include() a file of functions, but not its format (e.g., its own <?php...?> tags or not), which could confuse a beginner.

Page 125 warns the reader to change the value of session.save_path in php.ini, from "/tmp" to an existing directory on Windows, else the reader's sessions will fail. But I found no such change necessary, as the line in php.ini (copied from php.ini-recommended) was commented out by default.

One page 240, the author states that the user should have added the "--with-mysql" option to PHP's configure script. This statement will probably confuse most readers using Windows, since that option was only discussed in the section on installing Apache and PHP on Unix.

One page 283, the author suggests that anyone wanting to better understand a particular regular expression, should "consider reading the article on regular expressions", but it wasn't clear — at least to me — to what article he is alluding. Perhaps he means one of the Web pages referenced in the chapter 14 summary.

Throughout the code snippets that check for POST-ed form input, the test made is "if (! $_POST[ 'submit' ])", which generates an error, "Undefined index: submit", since the associative array $_POST[] has no key 'submit'. Safer is "if (! isset($_POST[ 'submit' ]))".

The publisher's website is missing a list of errata for the book, so perhaps none have yet been reported. Here are some errata that I found: On page 8, the word "résumé" is missing the first accent ague. On page 20, the last sample application listed is Gallery, at "http://gallery.menalto.org/", which is no longer a valid address (I'm assuming that it was when Vaswani wrote that chapter). On page 89, "until the values of $lowerLimit and $upperLimit are equal" should read "until the value of $lowerLimit exceeds that of $upperLimit" since $lowerLimit does reach 11. On page 97, "one or more values" should read "zero or more values". On page 123, "short listed" should be hyphenated. On page 125, "re-create" needs no hyphen. On page 128, ["NULL] should be ["] (single-quotes) or [NULL]. On page 131, "harmful code" should be "control characters". On page 135, "which the product's" should be "with the product's". "http://www.sporum.org/", on page 145, appears to have been abandoned. Pages 304 and 373 state that the newline character is "/n"; rather, it is "\n". On page 309, if "significant digits" is meant in a scientific sense, then the result would be "1" and not "1.67". Figure 15-8 has an erroneous edit cursor in the "Ila T" row. On page 324, quarter()'s range should be 1-4, not "1-2".

One problem with How to Do Everything with PHP and MySQL — outside the control of the author — is the poor manner in which the pages have been cut, in which every page seems to have a unique width, at least in the copy that I have. Consequently, when thumbing through the book, it is difficult to flip one page at a time, and instead the pages will usually flip in groups, causing the reader to miss some of the pages. This variable page width does not appear to be intentional on the part of the publisher, and does not seem to be a result of the pages being set badly within the adhesive, because those line up evenly. Rather, it may be due to sloppy page cutting after binding. Whatever the cause, it is rather annoying, and one can only hope not typical of Osborne's production quality in general or in the future.

Another weakness, common to technical trade paperbacks, is the way the pages are glued into the spine, attached to a cover made of laminated card stock. This so-called "adhesive binding" (or misnamed "perfect binding") prevents the book from laying completely flat when opened up. For large volumes opened near the middle of the book, the weight of the pages on either side can keep the book open — though not as flat as one would like. But the reader cannot do the same when trying to reference the book within the first or last 200 or so pages, because then the inflexible glue binding pulls on the lighter half enough to close the book on the frustrated reader. Unfortunately, the book under review, being medium-sized (or perhaps small, given today's standards), cannot be laid flat without breaking the binding. It is astonishing to me that so few publishers are taking advantage of flexible lay-flat bindings, such as RepKover, used by O'Reilly Media for some of their publications.

Aside from its many minor flaws, I recommend this title to any programmer who wants to learn the basics of PHP and MySQL. Even though the publisher could improve their production quality, choice of binding, and Web page, the author has done a good job of clearly presenting the major points. Future editions could incorporate fixes to the errors noted here, as well as better explain to the neophyte how to test/debug the code snippets. Nonetheless, the intended reader would be well served by this particular book.

Copyright © 2005 Michael J. Ross. All rights reserved.
bad bots block