1

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.

5
  • Store it in a variable and pass it around ? Commented Feb 17, 2016 at 16:31
  • Your query works? Because I think that $db is null... Commented Feb 17, 2016 at 16:32
  • Are you using MVC? Create it inside the __construct() method as a class variable inside your Model and reference it from there. If you're not using MVC, store the actual $db variable at the beginning of your code and reference it in your other code. Also, the connection function is not returning your new PDO object. I'm curious if that's functioning right now? Commented Feb 17, 2016 at 16:32
  • 1
    perhaps use the Singleton design pattern to create your DB object? Commented Feb 17, 2016 at 16:35
  • check out this article: code.tutsplus.com/tutorials/… Commented Feb 17, 2016 at 16:44

3 Answers 3

2

What if you make a config.php file that you include at the top of your page. You only need to create the object once, and all queries on the page can reference the same object:

function connection(){ $host = 'localhost'; $user = 'user'; $pass = 'password'; $dbName = 'db_name'; new PDO("mysql:host=$host; dbname=$dbName", $user, $pass); } $db = connection(); $query = 'Statement #1'; $stmt = $db->prepare($query); $stmt->execute(); $query = 'Statement #2'; $stmt = $db->prepare($query); $stmt->execute(); $query = 'Statement #3'; $stmt = $db->prepare($query); $stmt->execute(); $query = 'Statement #4'; $stmt = $db->prepare($query); $stmt->execute(); 
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this maybe

function connection(){ static $CONN; if (!$CONN) { $host = 'localhost'; $user = 'user'; $pass = 'password'; $dbName = 'db_name'; $CONN = new PDO("mysql:host=$host; dbname=$dbName", $user, $pass); } return $CONN; } 

This might do what you want without much extra change.

4 Comments

It's a sort of very basic pseudo-singleton implementation
yeah but why not do it properly?
I'm trying to help with the problem at hand. Maybe it's worth rewriting the entire codebase; maybe not.
At least use static instead of global.
0

Several people has mentioned static and the Singleton pattern. This is what the code would look like, keeping only one instance of the PDO object around at one time:

class DB{ private static $_instance; private $_pdo; public static function getInstance(){ if(self::$_instance === NULL) { $dsn = 'mysql:dbname=dbname;host=host'; $user = 'user'; $password = 'password'; // call constructor and assign instance self::$_instance = new self($dsn, $user, $password); } return self::$_instance; } /** * Creates new DB wrapping a PDO instance * * Constructor is private because this class can't be instantiated. * */ private function __construct($dsn, $user, $password){ try { $this->_pdo = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } } /** * Singletons may not be cloned */ private function __clone() {} /** * Delegate every method call to PDO instance * * @param String $method * @param Array $args * @return Mixed */ public function __call($method, $args) { return call_user_func_array(array($this->_pdo, $method), $args); } } // To instantiate: $db = DB::getInstance(); 

3 Comments

You're right. This was a quick demo, focused on development, not production.
"Never" means neither on deveopment or production. Nor in quick demos.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.