The first six chapters explain PHP essentials, including data types, functions, string manipulation, arrays and objects. Next comes a look at basic Web techniques, followed by an introduction to database access. There is a chapter on generating graphics with the GD extension library and another on creating Adobe PDF documents. The authors then show how to parse XML, and there is a section on security with some handy tips for protecting PHP sites. A chapter on application techniques looks at code libraries, performance tuning and handling errors. Next there is an explanation of how to build extensions to PHP using C, followed by a look at Windows issues such as COM and ODBC. Finally, there is a complete reference to the standard functions in PHP 4.0.
This is not an advanced programming book, but even experienced coders will discover new things about the language and get a clearer understanding of how PHP works. The specialist chapters such as those on XML or PHP extensions tend to be introductory, so readers will need further resources. For example, the database section is short, and would be best read alongside Web Database Applications with PHP and MySQL or another book with more detailed database coverage. Even so, this is a strong hands-on title that PHP developers will want to keep close at hand. ----Tim Anderson --This text refers to an out of print or unavailable edition of this title.
"Personally, I think the authors did a very credible job of creating a book that even a non-programmer can use to get started...." -- Jim Huddle, CompuNotes Issue #191
"The book is a useful and practical introduction for those who want to get started with PHP programming." -- Pete Hoyle, Williamsburg Macromedia User Group, August 2002 --This text refers to an out of print or unavailable edition of this title.
From the Publisher
About the Author
Rasmus Lerdorf started the PHP Project back in 1995 and has been actively involved in PHP development ever since. Also involved in a number of other open source projects, Rasmus is a longtime Apache contributor and foundation member. He is the author of the first edition of the PHP Pocket Reference, and the co-author of the first edition of Programming PHP.
Kevin Tatroe has been a Macintosh and Unix programmer for ten years. He is an experienced PHP developer, knowledgeable in the changes coming with PHP 5. He is also co-author of the first edition of Programming PHP.
Peter MacIntyre lives and works in Charlottetown, Prince Edward Island, Canada. He has over 16 years of experience in the information technology industry, primarily in the area of software development. Peter's technical skill set includes several client/server tools and relational database systems such as PHP, PowerBuilder, Visual Basic, Active Server Pages, and CA-Visual Objects.
Peter is certified by ZEND Corporation on PHP 4.x and has contributed writing material for Using Visual Objects (Que Corp.), Using PowerBuilder 5 (Que Corp.), ASP.NET Bible (Wiley Pub.), and Web Warrior Survey on Web Development Languages (Course Technology).
Peter is also currently a contributing editor and author to the on-line and in-print magazine called php|architect (www.phparch.com). He has also spoken several times at North American and International computer conferences including CA-World in New Orleans, USA; CA-TechniCon in Cologne, Germany; and CA-Expo in Melbourne, Australia.
Excerpt. © Reprinted by permission. All rights reserved.
PHP has support for over 20 databases, including the most popular commercial and open source varieties. Relational database systems such as MySQL, PostgreSQL, and Oracle are the backbone of most modern dynamic web sites. In these are stored shopping-cart information, purchase histories, product reviews, user information, credit-card numbers, and sometimes even web pages themselves.
This chapter covers how to access databases from PHP. We focus on the PEAR DB system, which lets you use the same functions to access any database, rather than on the myriad database-specific extensions. In this chapter, you ll learn how to fetch data from the database, how to store data in the database, and how to handle errors. We finish with a sample application that shows how to put various database techniques into action.
This book cannot go into all the details of creating web database applications with PHP.For a more in-depth look at the PHP/MySQL combination,see Web Database Applications with PHP and MySQL by Hugh Williams and David Lane (O Reilly).
Using PHP to Access a Database
There are two ways to access databases from PHP. One is to use a database-specific extension; the other is to use the database-independent PEAR DB library. There are advantages and disadvantages to each approach.
If you use a database-specific extension,your code is intimately tied to the database you re using.The MySQL extension s function names, parameters, error handling, and so on are completely different from those of the other database extensions. If you want to move your database from MySQL to PostgreSQL,it will involve significant changes to your code.The PEAR DB,on the other hand,hides the database-specific functions from you;moving between database systems can be as simple as changing one line of your program.
The portability of an abstraction layer like PEAR s DB library comes at a price however. Features that are specific to a particular database (for example, finding the value of an automatically assigned unique row identifier)are unavailable. Code that uses the PEAR DB is also typically a little slower than code that uses a database-specific extension.
Keep in mind that an abstraction layer like PEAR DB does absolutely nothing when it comes to making sure your actual SQL queries are portable. If your application uses any sort of non generic SQL, you ll have to do significant work to convert your queries from one database to another.For large applications,you should consider writing a functional abstraction layer;that is,for each database your application needs to support, write a set of functions that perform various database actions, such as get_user_record(),insert_user_record(),and whatever else you need, then have a configuration option that sets the type of database to which your application is connected. This approach lets you use all the intricacies of each database you choose to support without the performance penalty and limitations of an abstraction layer. This would, however, take quite some time to build from scratch.
For simple applications, we prefer the PEAR DB to the database-specific extensions, not just for portability but also for ease of use. The speed and feature costs are rarely significant enough to force us into using the database-specific extensions. For the most part, the rest of this chapter gives sample code using the PEAR DB abstraction
For most databases, you ll need to recompile PHP with the appropriate database drivers built into it. This is necessary whether or not you use the PEAR DB library. The help information for the configure command in the PHP source distribution gives information on how to build PHP with support for various databases. For example:
--with-mysql [=DIR ] Include MySQL support..DIR is the MySQL base
directory.If unspecified,the bundled MySQL
library will be used.
--with-oci8 [=DIR ] Include Oracle--oci8 support.Default DIR is
--with-ibm-db2 [=DIR ] Include IBM DB2 support..DIR is the DB2 base
install directory,defaults to
--with-pgsql [=DIR ] Include PostgreSQL support..DIR is the PostgreSQL
base install directory,defaults to
You can t build PHP with support for a database whose client libraries you don t have on your system. For example, if you don t have the Oracle client libraries, you can t build PHP with support for Oracle databases.
Use the phpinfo()function to check for database support in your installation of PHP. For instance, if you see a section in the configuration report for MySQL, you know you have MySQL support.
New in PHP Version 5 is the compact and small database connection called SQLite. As its name suggests, it is a small and light weight database tool. This database product comes with PHP 5 and has replaced the default database tool that once was MySQL. You can still use MySQL with PHP, but you have to do a little work to get it set up. SQLite is ready to go right "out of the box "when you install PHP, so if you are looking for a light weight and compact database tool, then be sure to read up on SQLite.