A data source name (DSN) is a string that specifies where the database is located, what kind of database it is, the username and password to use when connecting to the database, and more. The components of a DSN in PEAR are assembled into a URL-like string:
The only mandatory field istype, which specifies the PHP database backend to use. Table 8-1 lists the implemented database types at the time of writing.
Table 8-1. PHP database types
MySQL (for MySQL >= 4.1)
Microsoft SQL Server
Theprotocol is the communication protocol to use. The two common values are"tcp"and"unix,"corresponding to Internet and Unix domain sockets. Not every database backend supports every communications protocol.
In Example 8-1, we connected to the MySQL databaselibrarywith the usernamelibrarianand passwordpassw0rd.
A common development technique is to store the DSN in a PHP file and include that file in every page that requires database connectivity. Doing this means that if the information changes, you donít have to change every page. In a more sophisticated settings file, you might even switch DSNs based on whether the application is running in development or deployment mode.
Please check back next week for the continuation of this article.