[TR]
|
[TD]
|
// The pear base directory must be in your include_path
|
require_once 'DB.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";
|
// DB::connect will return a Pear DB object on success
|
// or a Pear DB Error object on error
|
// You can also set to TRUE the second param
|
// if you want a persistent connection:
|
// $db = DB::connect($dsn, true);
|
$db = DB::connect($dsn);
|
// With DB::isError you can differentiate between an error or
|
// a valid connection.
|
if (DB::isError($db)) {
|
die ($db->getMessage());
|
}
|
....
|
// You can disconnect from the database with:
|
$db->disconnect();
|
?> [/TD][/TR] |
[TR]
|
[TD] * phptype: Database backend used in PHP (mysql, odbc etc.)
|
* dbsyntax: Database used with regards to SQL syntax etc.
|
* protocol: Communication protocol to use (tcp, unix etc.)
|
* hostspec: Host specification (hostname[:port])
|
* database: Database to use on the DBMS server
|
* username: User name for login
|
* password: Password for login
|
*
|
* The format of the supplied DSN is in its fullest form:
|
*
|
* phptype(dbsyntax)://username:password@protocol+hostspec/database
|
*
|
* Most variations are allowed:
|
*
|
* phptype://username:password@protocol+hostspec:110//usr/db_file.db
|
* phptype://username:password@hostspec/database_name
|
* phptype://username:password@hostspec
|
* phptype://username@hostspec
|
* phptype://hostspec/database
|
* phptype://hostspec
|
* phptype(dbsyntax)
|
* phptype[/TD][/TR] |
[TR]
|
[TD]
|
// Once you have a valid DB object
|
...
|
$sql = "select * from clients";
|
// If the query is a "SELECT", $db->query will return
|
// a DB Result object on success.
|
// Else it simply will return a DB_OK
|
// On failure it will return a DB Error object.
|
$result = $db->query($sql);
|
// Always check that $result is not an error
|
if (DB::isError($result)) {
|
die ($result->getMessage());
|
}
|
....
|
?> [/TD][/TR] |