Three ways to use PHP to work with MySQL

PDO (PHP Data Objects)

PDO is relatively new to PHP, and it has several advantages over the older ways of working with a database. The main advantage of PDO is that it defines a consistent object-oriented interface that supports most popular databases. As a result, PDO allows you to write code that is portable across different database servers such as Oracle, DB2, Microsoft SQL Server, PostgreSQL, and others.

One disadvantage of PDO is that it doesn’t take advantage of all of the advanced features found in MySQL 4.1.3 and later, such as multiple statements. For most web applications, that isn’t an issue. However, if you need to take advantage of these features, you can use the mysqli extension.

PHP mysqli extension

The mysqli extension (the i is for improved) is included with PHP 5 and later. This extension provides an object-oriented interface and a procedural interface. One advantage of mysqli is that it takes advantage of all new features found in MySQL 4.1.3 and later. As a result, if you need to use these features, you can use mysqli instead of PDO.

One disadvantage of mysqli is that it is designed to work with the MySQL database and can’t be used with other database servers. As a result, if you want to write code that can be used with other database servers, you should use PDO instead of mysqli.

PHP MYSQL extenstion

The MySQL extension is the oldest PHP interface for working with MySQL and has been deprecated as of PHP 5.5. As a result, you’ll only want to use this extension if you’re using an old version of PHP or maintaining legacy code. If you need to use it, it is similar to the procedural version of the mysqli extension that’s described later in this chapter.

Description

Back