Home arrow PHP arrow Page 4 - Databases and PHP

Data Source Names - PHP

If you're interested in learning how to access databases from PHP, you've come to the right place. This four-part series will focus on the PEAR DB system. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Databases and PHP
  2. Relational Databases and SQL
  3. PEAR DB Basics
  4. Data Source Names
By: O'Reilly Media
Rating: starstarstarstarstar / 8
June 14, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

  type(dbsyntax)://username:password@protocol+hostspec/ database

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

Name Database
Mysql MySQL
mysqli MySQL (for MySQL >= 4.1)
Pgsql PostgreSQL
Ibase InterBase
Msql Mini SQL
Mssql Microsoft SQL Server
oci8 Oracle 7/8/8i
Odbc ODBC
Sybase SyBase
Ifx Informix
Fbsql FrontBase
Dbase DBase
Sqlite SQLite

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.

These are some sample valid data source names:

  mysql:///library
  mysql://localhost/library
  mysql://librarian@localhost/library
  mysql://librarian@tcp+localhost/library
  mysql:// librarian:passw0rd@localhost/library

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.



 
 
>>> More PHP Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: