Right now every time i need to run a query i call a function named connection() that creates a new PDO object.
function connection(){ $host = 'localhost'; $user = 'user'; $pass = 'password'; $dbName = 'db_name'; new PDO("mysql:host=$host; dbname=$dbName", $user, $pass); } $db = connection(); $query = 'SELECT ...'; $stmt = $db->prepare($query); $stmt->execute(); The problem is the web site takes exactly 1 second to create this object and, as you can image, if i need to run 4 queries the page will load in 4 seconds. So, is there any way i can store this PDO object to optimize performance?
I'm using this function in multiple files so i need it.
$dbis null...__construct()method as a class variable inside your Model and reference it from there. If you're not using MVC, store the actual$dbvariable at the beginning of your code and reference it in your other code. Also, theconnectionfunction is not returning your new PDO object. I'm curious if that's functioning right now?