Jump to content

Best Way to Use PDO


imperium2335

Recommended Posts

Hi,

 

I am trying to deside which is the best way to handle database connection methods for my new web app.

 

Should I do what I always do, which is

 include('dbconnect.php');

which has the lines:

$options = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING) ;

try{
$dbh = new PDO("mysql:host=localhost;dbname=chat;", 'xxxx', 'xxxxx', $options) ;
$dbh->exec("SET NAMES utf8") ;
}catch(Exception $e) {
die('Database connection failed. (' . $e . ')') ;
}

 

Or should I make my own function like this:

function dbConnect($user, $pass, $host = 'localhost', $db = 'chat') {
try{
	$dbh = new PDO("mysql:host=$host;dbname=$db;", $user, $pass) ;
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
	$dbh->exec("SET NAMES utf8") ;
}catch(Exception $e) {
	die('Database connection failed. (' . $e . ')') ;
}
return $dbh ;
}

 

Which way is more efficient?

Link to comment
Share on other sites

It doesn't really matter. All preference.

 

Watch out though, because if $option is defined, including dbconnect.php will trash it.

 

Also, be very careful and know what you're doing before using persistent connections.

Link to comment
Share on other sites

As far as efficiency goes, the top is ever-so-slightly more efficient seeings there isn't an extra function call. But the difference is negligible, so I wouldn't worry about it.

 

Writing out prepared statements for every single query is annoying and messy, in my opinion. I always prefer using wrapper classes to make things neat and tidy.

Link to comment
Share on other sites

A 'wrapper' is simply extending the functionality of a given function. You could make a wrapper to automatically check a query for errors.

function mysql_query_wrapped( $q, $link = NULL ) {
$res = mysql_query( $q, $link );
if( $res == FALSE ) {
	echo 'Error: '.mysql_error($link);
	return FALSE;
} else
	return $res;
}

 

Not the greatest example, but it explains what a basic wrapper would do.

 

As far as persistent connections go, they could end up slowing down your script. It's something you should avoid unless you need it, and if you do use it, understand EXACTLY what keeping connections open can mean, how it works, and how to avoid the problems that can come from it. That's not really something that can be explained only through a forum.

 

Is this dozens of queries per request, or dozens of requests per second?

 

Also, I want to be able to switch between 2 different DBs on the same server, would a function like my example above be the best way for this?

 

It appears with PDO, you'll need a separate instance of the class to access a second database.

 

Alternately, you can use the syntax (MySQL) INSERT INTO dbname.tablename SET `col` = 'value'

Link to comment
Share on other sites

It's for a simple chat room. I don't know JAVA well at all nor OOP, so I'm settling for AJAX to make want I want. Could something be done with sockets to determine if a new message has been posted, then update the display? Instead of polling the database every second which is where the bulk of queries are.

 

Another problem I thought of was how to detect if a user has gone offline, apart from having a 5 minute time out based on when they last posted?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.