Introduction - Connect -- Connecting and disconnecting a database
Description
To connect to a database you have to use the function MDB::connect(), which requires a valid DSN as parameter and optional a boolean value, which determines wether to use a persistent connection or not. In case of success you get a new instance of the database class. It is strongly recommened to check this return value with MDB::isError(). To disconnect use the method disconnect() from your database class instance.
<?php require_once 'MDB.php'; $user = 'foo'; $pass = 'bar'; $host = 'localhost'; $db_name = 'clients_db'; // Data Source Name: This is the universal connection string $dsn = "mysql://$user:$pass@$host/$db_name"; // MDB::connect will return a PEAR MDB object on success // or an PEAR MDB Error object on error $db = MDB::connect($dsn); // With MDB::isError you can differentiate between an error or // a valid connection. if (MDB::isError($db)) { die ($db->getMessage()); } .... // close conection $db->disconnect(); ?>
Deliver First Class Web Sites: 101 Essential Checklists Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments?