I have a question about using PDO to which I could not find the answer. This probably works for mysql / mysqli connections.
In the main inclusion of my project, I create a PDO object using
$pdo = new PDO('connection here');
Well, I have a class that needs access to the database. So instead of using "global $ pdo;" inside each function, I did the following.
class MyClass(){ private $db = null; __construct(){ global $pdo; $this->db = $pdo; } function example(){ $sql = 'A Query'; $this->db->prepare($sql); } }
In any case, my question is: does this make 2 connections to the database, since I effectively duplicate $ pdo by setting the $ db var class for it? The main reason I ask is because I see this happening a lot on our system, and I'm interested in creating too many MySQL connections and ridding the system of unnecessary connections.
As part two, does the next reason have duplication, and can I go through ref? I'm a little afraid to try and make something break.
Change
function MyFunction($member_id, $pdo){
For
function MyFunction($member_id, &$pdo){
Thanks!
source share