Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ed white
13,291 PointsPHP/MySQL - PDO - best practice for opening / using?
I'm messing around with building something based off the mike's t-shirts site from the PHP/MySQL project... and following the same way of doing any db queries from function in the products file.. doing a require_once... in each function call to open a PDO $db object and perform the query.. But I was finding the db queries were slowing down page loads significantly..
After some googling I found the PDO::ATTR_PERSISTENT => true attribute that can be added to the PDO constructor... and that has sped up the pages loads back to 'normal..
But is this the 'normal' practice.. is there a better way to be opening and using the PDO object rather than doing a require() inside every function call that makes a db call? in 'real-world' scenarios, how should a php app handle it's PDO objects and db calls?