#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221

    Pass PDO Connection Between Functions Like A Basketball


    Hi;

    How can I do this without having to use classes (and singleton, DI etc)?

    Possible to do it only with functions (fully procedural )or even session?

    PHP Code:
    function connect_to_db()
        {
            
    $conn = new PDO("mysql:host=".SEREVERNAME.";dbname=".DATABASE.""DB_USERNAMEDB_PASSWORD);
            
    $conn $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            
    $_SESSION['connection'] = $conn;
        }
    function 
    one()
        {
            
    $stmt $_SESSION['connection']->prepare("SQL");
            
    $stmt->bindParam(':things'$things);
            
    $stmt->execute();
        }
    function 
    two()
        {
            
    $stmt $_SESSION['connection']->prepare("SQL 2");
            
    $stmt->bindParam(':things'$things);
            
    $stmt->execute();
        } 
    PHP Code:
    function connect_to_db()
        {
            if(!
    existing_connection)
                {
                    
    $conn = new PDO("mysql:host=".SEREVERNAME.";dbname=".DATABASE.""DB_USERNAMEDB_PASSWORD);
                }   
            else 
                {
                    
    $conn existing_conneciton;
                }     
            return 
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        }
    function 
    one()
        {
            
    $stmt connect_to_db()->prepare("SQL");
            
    $stmt->bindParam(':things'$things);
            
    $stmt->execute();
        }
    function 
    two()
        {
            
    $stmt connect_to_db()->prepare("SQL 2");
            
    $stmt->bindParam(':things'$things);
            
    $stmt->execute();
        } 
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,472
    Rep Power
    2105
    Pretty easy. And you've almost got it.


    PHP Code:
    function connect_to_db () {
        
    $conn = new PDO ("mysql:host=".SEREVERNAME.";dbname=".DATABASE.""DB_USERNAMEDB_PASSWORD);
        
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
        return 
    $conn;
    }

    $conn connect_to_db ();

    function_1 ($conn);
    function_2 ($conn); 
    The problem that you had with your examples is that you're setting $conn to the returned value of the setAttribute() call, which is a boolean, not the connection object.
  4. #3
  5. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Thanks.

    Any issues with SESSION?

    P.S I am still in Brisbane
  6. #4
  7. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,903
    Rep Power
    9646
    You can't put resources into the session. If you put it in $_SESSION then it won't work on the next page and all you're doing is making a registry for the current page to use.

    Just pass it as an argument.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,274
    Rep Power
    4193
    There's also the function equivalent of a singleton.
    Code:
    function connect_to_db () {
        static $conn;
        if (!$conn){
            $conn = new PDO ("mysql:host=".SEREVERNAME.";dbname=".DATABASE."", DB_USERNAME, DB_PASSWORD);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }    
    
        return $conn;
    }
    The first time you call connect_to_db it'll make the connection. Every other time will just return the existing connection.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo