The folks at Packt Publishing sent me a copy of Dennis Popel’s “Learning PHP Data Objects” to review. I’d never heard of Packt before, and was intrigued. They’re a relatively new publisher, with a growing range of books on PHP-related topics, and they remind me of what O’Reilly was like when they first started out. According to their About Us page, they also pay a royalty direct to open source projects – so sales from Dennis’ book should result in money coming the PHP Group’s way.
What Does The Book Claim To Do?
The jacket of the book says that, by reading the book, you’ll learn:
- An overview of the technology
- Getting started with PDO
- Creating sample databases
- Different methods of creating connection strings
- Error handling, prepared statements, and handling rowsets
- Getting column metadata
- Setting connection parameters with examples
- Handling rowsets and different ways to retrieve multiple rows
The book also includes a helpful appendix on PHP object-oriented programming for readers who aren’t familiar with using PHP this way.
My copy of the book doesn’t include a CD with the code listings on, and I wasn’t able to find the code listings online to download. That’s something I’d like to see Packt change in the next edition; I thought we’d stopped having to type in code examples with the demise of the good ol’ ZX Spectrum 🙂 I haven’t tested the example code in the books as a result, and given Lukas’s concerns about errors in the code found in another of Packt’s books, don’t be surprised if there are issues here. But don’t be put off by it either.
Does It Do What It Says On The Tin?
If you’ve never used PHP’s PDO before, and you’re looking for a book to get you up and running, then you should take a look at Learning PHP Data Objects. The book covers all the basics, plus error handling (something I personally heartily approve of 🙂 ), and also topics like streaming BLOB objects from the database out to the browser.
This isn’t a book that aims to teach you SQL, nor the differences between the different SQL dialects supported by your favourite database platforms; it’s totally focused on the PDO side of things. This is a good thing; it ensures that the examples and text aren’t constantly full of tony tangents distracting you from the main reason for buying this book: to learn PDO.
Is This A Book You Will Go Back To In The Future?
I’m not sure. From start to finish, the book is written as a tutorial rather than a reference, staying true to the book’s title and mission. I feel that the book’s index is incredibly weak, which is a shame, but not unexpected in a tutorial book. It’s the sort of book that you’d like to have on the company’s bookshelf to help new starters get up to speed with PDO, but it’s likely to stay on the shelf afterwards.
If you’re new to PHP, or haven’t yet made the move from PHP 4 to PHP 5, then this book can help you get started with one of the important new features of PHP 5. It’s a book that does exactly what it says on the tin, which is a refreshing change 🙂
Whilst there’s plenty of online tutorials about PDO out there, for the folks who prefer to read a real book, as far as I can tell after a quick bit of Googling this is the only book out there to date that’s dedicated solely to covering PDO (if you know of any such books, please leave a link to them in the comments below).
If you’ve bought this book and have an opinion on it, please leave a comment.
Be the first to leave a comment »
I’ve just seen Wez’s post about Microsoft’s new Community Technology Preview for their SQL Server 2005 extension for PHP.
Why Is It Important For Microsoft To Support PHP On Windows?
I can’t speak for the rest of the world, but Windows is an important platform here in the UK. About half the customers I work with run their websites on Windows Server. Every customer who prefers Windows Server over Linux automatically wants to use the full Microsoft stack, and that means IIS and Microsoft SQL Server.
Starting with Windows Server 2003 Service Pack 1, IIS became a viable platform for running production PHP sites on. SP1 has a fix for a nasty problem in the Windows kernel which stopped PHP working well as a CGI program under IIS. It’s true, IIS has serious limitations compared to Apache which cost time and money to overcome – chiefly no out-the-box equivalent to mod_rewrite – but it performs well and is simple enough for many folks to admin more easily than Apache (although the new IIS7 admin interface might be a step backwards there). With the new FastCGI support recently released, it’s now possible to further optimise an IIS setup too.
SQL Server is a true heavyweight database server. The most commonly deployed version that I come across is SQL Server 2000, although SQL Server 2005 is starting to appear in various enterprises. It is fast, scalable (in the old-fashioned way unfortunately, which is expensive), and when it comes to old-fashioned replication (as opposed to scale-out) I think that it leaves MySQL for dead. Although expensive to buy outright, if you buy your hosting boxes from places like Rackspace, the cost tends to be much more reasonable.
PHP comes with an MSSQL extension that’s based on the old TDS data protocol, which I believe is a legacy protocol from the old Sybase days (SQL Server is based on Sybase). TDS is also supported on Linux through the FreeTDS project, which allows PHP running on Linux boxes to connect to Microsoft SQL Server running on Windows.
What’s Wrong With The Existing MSSQL Extension For PHP?
… or, why do we need an improved SQL Server extension for PHP? 🙂
The existing MSSQL extension works well, but has a few practical limitations that have to be worked around.
- Limited to varchar(255) support. SQL Server 2000 and later support varchar columns longer than 255 bytes in size, but unfortunately the old TDS-based MSSQL extension can only support up to varchar(255).
- No support for unicode columns like nvarchar. The size of a varchar column is specified in bytes, not characters. If you’re working with UTF8 or UTF16 encoded data, one non-ASCII character takes up multiple bytes of space. This cuts down on the amount of characters you can store in a varchar field, and it makes things like HTML form validation – er – interesting. nvarchar, by contrast, is advertised as a variable-size datatype for storing multi-byte characters. nvarchar(255) holds 255 characters, not 255 bytes.
- No PDO drivers. Although there’s some debate about the performance merits of PDO, PDO’s prepared statement support is a real boon when it comes to preventing SQL injection attacks.
- Poor error reporting. The MSSQL extension doesn’t provide an equivalent to mysql_error() et al, which is a bit of a pain.
At the moment, I’ve no idea whether Microsoft’s extension addresses any of these issues. There’s no documentation online, just a .exe file that isn’t going to run under OS X 🙂 I’ll have a look at it when I get to my Windows PC at work, and see what it can – and can’t – do.
15 comments »
Many thanks to everyone who commented on my recent article and said they’d be interested in a series of posts about more server-oriented PHP topics. There were quite a few requests for a “ten point”-type article introducing the subject, so that seems to be a good place to kick things off.
There are six classic ways to group and organise the servers that your web-based application runs on.
Shared hosting, like the name implies, is where you cram many different websites (normally owned by many different people) onto the same physical box. The upside is that they’re very cheap (because you’re not paying for an entire server, only a slice of one), but the downsides are that you can quickly outgrow a shared hosting server, and shared hosting servers are difficult to make really secure. Use them when you have to, but do your best to trade up to something that you can have all to yourself whenever you can.
A dedicated server is a box all to yourself. So long as you can keep the bad guys outside, you have more peace of mind when it comes to security. They cost a bit more than shared hosting, but there are many affordable solutions both in Europe and the US.
A PHP application normally needs no changes at all to move from shared hosting to a dedicated server.
What might need to change is the way you look after the installed application. Unless you go for a managed server, or you are using a server looked after by your customer, it will become your responsibility to look after the operating system installed on the server. You will be responsible for ensuring the server is patched with the latest security fixes. This can eat up quite a bit of time every week, so make sure your customer is paying for this time one way or another!
When you outgrow a single server, the next step is to consider moving to a separate database server and web server, also known as a two-tier architecture. This is a popular choice because it’s quick and very painless (just order an additional box, and then move your database onto it), and it buys you the time you need to prepare for scaling up to the next architecture. Even on commodity Intel/AMD servers (I’m talking Xeons and Opterons here, not desktop CPUs!), a two-tier architecture is often enough to handle a public website for a medium-to-large organisation.
The only change a PHP application should need is to update the hostname passed to mysql_connect() et al. Consider moving any background batch jobs you have onto the database server, to further reduce the load on the web server.
It’s a good idea at this point to split your PHP application into separate publishing and admin components, so that you can move the admin website onto the database server. Splitting these up allows the admin site to function well when the main website is being hit hard, but to make it work you’ve got to start thinking about how to share data between the publisher and admin components – the data that your website publishes, and your sessions too.
Once you’ve outgrown the two tier architecture, you can add more capacity on the publishing side by moving to a web farm, and you can make your database server more resilient by upgrading to a cluster. If you don’t mind the extra complexity and the reworking involved, you can also scale further by moving to an n-tier architecture.
Be the first to leave a comment »
On Planet PHP, I see a lot of postings about PHP the language, and applications written in PHP, but not a lot about the runtime environment that PHP needs and can take advantage of. Is anyone interested in a series of posts about server architectures – not just LAMP vs WIMP vs WISP (Linux/Apache/MySQL/PHP, Windows/IIS/MySQL/PHP, and Windows/IIS/SQL Server/PHP) – but also about shared hosting vs dedicated hosting vs server farms vs active-active, active-passive clusters?
If you are interested, leave a comment on my blog to let me know. If there’s enough interest, I’ll write up a few articles about this and publish them on my blog. If there’s anything in particular you’d like to see covered, and explained in more detail, let me know!
(One of the things I do is design hosting solutions for our customers. Not the mega-solutions that folks like Rasmus gets to work on, but the smaller – and way more common – solutions needed for small to medium-size websites here in the UK).
22 comments »