The most popular database platform used with PHP is the MySQL database. If you look at the MySQL website (www.mysql.com/) you will discover that there are a few different versions of MySQL you can use. We will look at the freely distributable version known as the community server. PHP has a number of different interfaces to this database tool as well, so we will look at the object-oriented interface known as MySQLi, a.k.a. the MySQL Improved extension. If you are not overly familiar with OOP interfaces and concepts, be sure to review Chapter 6 before you get too deeply into this section.
Since this object-oriented interface is built into PHP with a standard installation configuration (you just have to activate the MySQLi extension in your PHP environment), all you have to do to start using it is instantiate its class, as in the following code:
$db = new mysqli(
In this example, we have a database named
library, and we will use the fictitious username
petermac and the password of
1q2w3e9i8u7y. The actual code that
would be used is:
This gives us access to the database engine itself within the PHP
code; we will specifically access tables and other data later. Once this
class is instantiated into the variable
$db, we can use methods on that object to do our
A brief example of generating some code to insert a new book into the library database ...