At first i used this code to connect to database.

Code:
<?php // db_connect.php

$host = 'localhost';
$user = 'user';
$pass = 'password';

function db_connect($db='') {
    global $host, $user, $pass;
    
    $dbcn = @mysql_connect($host, $user, $pass)
        or die('The site database appears to be down.');

    if ($db!='' and !@mysql_select_db($db))
        die('The site database is unavailable.');
    
    return $dbcn;
}
?>
I used to call the db_connect("DB name") function from other scripts.

And now i want to shift to PDO.
so i did this.

Code:
<?php

// db_connect.php

$host = 'localhost';
$user = 'user';
$pass = 'password';

function db_connect($db='') {
    global $host, $user, $pass;

try {
    $dbh = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
return $dbh;
}
?>
But..i doesnt work..."Database is not selected error".
Is there any other method to connect to DB using PDO and separate function for DB connection?
Help.
Thnks.