imperium2335 Posted December 3, 2011 Share Posted December 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/ Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294052 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294053 Share on other sites More sharing options...
imperium2335 Posted December 3, 2011 Author Share Posted December 3, 2011 I'm don't know that much about persistent connections, just read they make things faster. My app will be making potentially dozens of inserts/updates per second, so I thought it would be a good idea to turn them on? Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294056 Share on other sites More sharing options...
imperium2335 Posted December 3, 2011 Author Share Posted December 3, 2011 Whats a wrapper you speak of? I'm still a bit green with OOP, but I know about basic classes etc. My web app is probably going to be FOP based mostly. Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294062 Share on other sites More sharing options...
imperium2335 Posted December 3, 2011 Author Share Posted December 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294064 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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' Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294065 Share on other sites More sharing options...
imperium2335 Posted December 3, 2011 Author Share Posted December 3, 2011 dozens of insert and update queries per second, at random. so it could be really quiet, but at peak times there could be loads and loads. Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294067 Share on other sites More sharing options...
imperium2335 Posted December 3, 2011 Author Share Posted December 3, 2011 BTW it is AJAX based using a looping $.post Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294086 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 The HTTP protocol wasn't designed for that kind of behavior. It's entirely possible, but if things start to slow down you'll have a lot of reading and testing to do Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294089 Share on other sites More sharing options...
imperium2335 Posted December 5, 2011 Author Share Posted December 5, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252401-best-way-to-use-pdo/#findComment-1294542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.