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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: