/    Sign up×
Community /Pin to ProfileBookmark

Best practices for db access in PHP5

Hi,

I really wanted to get an idea of the way people handle their database connections and queries in PHP5.

I’m not really sure of the best approach.
What I’m doing at the moment is defining a singleton class to hold a PDO connection object, then defining a class with static methods for querying:

[code=php]
// Singleton class to hold db connection
class db
{
private static $_instance;
private static $_connection;
private static $_user=’myUser’;
private static $_pass=’myPass’;
private static $_host=’myHost’;
private static $_dbname=’myDB’;

protected final function __construct()
{
try
{
self::$_connection = new PDO(‘mysql:host=’.db::$_host.’;dbname=’.db::$_dbname, db::$_user, db::$_pass, array(PDO::ATTR_PERSISTENT => true));
}
catch (PDOException $e)
{
print “Error!: ” . $e->getMessage() . “<br/>”;
die();
}
}

public static function getInstance()
{
if (!self::$_instance instanceof self)
{
self::$_instance = new self;
}
return self::$_instance;
}

public function con()
{
return self::$_connection;
}
}
[/code]

[code=php]
// Data Access Layer
class dal
{
private static function getResults($query)
{
return db::getInstance()->con()->query($query,PDO::FETCH_ASSOC);
}
private static function getResult($query)
{
return db::getInstance()->con()->query($query,PDO::FETCH_ASSOC)->fetch();
}

public static function getProducts()
{
$sql = ‘select * from products’;
return self::getResults($sql);
}

}
[/code]

What do you think of this approach? Is there a better way?

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 05.2009 — I prefer to pass the database object as a constructor parameter to those classes that need it.
[code=php]
class Model
{
private $db;

public function __construct(PDO $db)
{
$this->db = $db;
}
}

$db = new PDO($dsn);
$model = new Model($dsn);
$anotherModel = new Model($dsn);
[/code]

This still gives you only one PDO instance (remember that by default PHP5 passes object [i]references[/i] to functions), while keeping the class interfaces clear and easy to debug; and it also allows you to have separate PDO instances should you ever have a reason to need more than one.

Of course, my real preference is to use a framework (such as CodeIgniter) where stuff like this is handled "behind the scenes" and I don't have to worry about it. ?
×

Success!

Help @Black_Knight spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...