I am new to PDO and I have pretty simple question. I have a simple function for connecting to DB:
function connectDB()
{
try {
$dbh = new PDO('mysql:host='.Config::$db_server.';dbname='.Config::$db_name, Config::$db_login, Config::$db_password, array(
PDO::ATTR_PERSISTENT => true
));
$dbh->exec("SET CHARACTER SET utf8");
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
After calling this function I successfully connect to db. Later when trying to send a query using $dbh->query I got "Call to a member function query() on a non-object ". I do understand that - I don't have an instance of the class at the moment. But the only think to achieve that is to use $dbh = new PDO("settings") again, which is kind of stupid isn't? The function has no sense than. I tried to return the $dbh in the connectDB function (before the NULL statement) but it wasn't really working.
How should be this done properly?