Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Php pdo

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Php pdo

    I found the following on PHP.net, I need some help understanding a portion of it:

    To avoid exposing your connection details should you fail to remember to catch any exception thrown by the PDO constructor you can use the following class to implicitly change the exception handler temporarily.
    Code:
    <?php
    
    Class SafePDO extends PDO {
     
            public static function exception_handler($exception) {
                // Output the exception details
                die('Uncaught exception: ', $exception->getMessage());
            }
     
            public function __construct($dsn, $username='', $password='', $driver_options=array()) {
    
                // Temporarily change the PHP exception handler while we . . .
                set_exception_handler(array(__CLASS__, 'exception_handler'));
    
                // . . . create a PDO object
                parent::__construct($dsn, $username, $password, $driver_options);
    
                // Change the exception handler back to whatever it was before
                restore_exception_handler();
            }
    
    }
    
    // Connect to the database with defined constants
    $dbh = new SafePDO(PDO_DSN, PDO_USER, PDO_PASSWORD);
    
    ?>
    The very last bit:

    Code:
    $dbh = new SafePDO(PDO_DSN, PDO_USER, PDO_PASSWORD);
    Are PDO_DSN, PDO_USER, and PDO_PASSWORD just the database connection information?

    As this is done:

    Code:
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

  • #2
    New Coder
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    80
    Thanks
    1
    Thanked 8 Times in 8 Posts
    yes, the variables $dsn, $username, and $password are all set when you create the SafePDO class and are sent back to the parent PDO class through this line

    PHP Code:
                // . . . create a PDO object
                
    parent::__construct($dsn$username$password$driver_options); 
    This line is basically

    PHP Code:
    new PDO($dsn$username$password$dirver_options); 

  • Users who have thanked Truffle for this post:

    HDRebel88 (05-24-2012)

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Truffle View Post
    yes, the variables $dsn, $username, and $password are all set when you create the SafePDO class and are sent back to the parent PDO class through this line

    PHP Code:
                // . . . create a PDO object
                
    parent::__construct($dsn$username$password$driver_options); 
    This line is basically

    PHP Code:
    new PDO($dsn$username$password$driver_options); 
    Thanks.

    Now would adding the following functions on the end make any sense? Or could I somehow include them right in the class?

    PHP Code:
    <?php

    Class SafePDO extends PDO {
     
            public static function 
    exception_handler($exception) {
                
    // Output the exception details
                
    die('Uncaught exception: '$exception->getMessage());
            }
     
            public function 
    __construct($dsn$username=''$password=''$driver_options=array()) {

                
    // Temporarily change the PHP exception handler while we . . .
                
    set_exception_handler(array(__CLASS__'exception_handler'));

                
    // . . . create a PDO object
                
    parent::__construct($dsn$username$password$driver_options);

                
    // Change the exception handler back to whatever it was before
                
    restore_exception_handler();
            }
        
    }

    // Connect to the database - The other variables ($host, $user, $pass) will be changed to text later, that's why they're not included in the function argument. 
    function SafePDOPersist($dbname){
    $DB = new SafePDO("mysql:host=$host;dbname=$dbname"$user$pass, array(PDO::ATTR_PERSISTENT => true));
    }

    function 
    SafePDOCOE($dbname){
    $DB = new SafePDO("mysql:host=$host;dbname=$dbname"$user$pass);
    }

    ?>
    Do I need to add a return to the functions SafePDOCOE and SafePDOPersist?
    Last edited by HDRebel88; 05-24-2012 at 09:32 PM.

  • #4
    New Coder
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    80
    Thanks
    1
    Thanked 8 Times in 8 Posts
    I'm not exactly sure what you are trying to do

    I don't see the need for having functions in there or to add anything to your class. Ideally the SafePDO class is in a separate file called SafePDO.php and is the only thing in that file

    This line does everything you want it to
    PHP Code:
    $DB = new SafePDO("mysql:host=$host;dbname=$dbname"$user$pass, array(PDO::ATTR_PERSISTENT => true)); 
    If you are afraid that you'll be typing that over and over and want something shorter then you could create a new class called SafePDOPersistent that extends SafePDO and in the constructor always the flag to true

    PHP Code:
    class SafePDOPersistent extends SafePDO
    {
        function 
    __construct($host$dbname$user$pass)
        {
            
    parent::SafePDO("mysql:host=$host;dbname=$dbname"$user$pass, array(PDO::ATTR_PERSISTENT => true));
        }

    Then whenever you want a persistent connection type this
    PHP Code:
    $DB = new SafePDOPersistent('localhost''dbanme''username''password'); 

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    I might note that the SavePDO class is only useful, if you keep PDO’s default error handling. as soon as you set PDO to use Exceptions (via PDO::ATTR_ERRMODE), this all becomes obsolete as you have to use try...catch anyways (or a custom Exception handler)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Dormilich View Post
    I might note that the SavePDO class is only useful, if you keep PDO’s default error handling. as soon as you set PDO to use Exceptions (via PDO::ATTR_ERRMODE), this all becomes obsolete as you have to use try...catch anyways (or a custom Exception handler)
    Is there any thing I can add to the class to cover that?

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    if you set PDO to use Exceptions for its error handling, you explicitly intend to use try…catch. everything else defeats the purpose of exception handling (it doesn’t make sense to use Exceptions without try…catch).

    it’s like having an UPS (uninterruptible power supply) and only switching it on for the startup of your systems and then turning it off.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Would this make any sense? Building in try/catch on the connection into an class extension?

    This is the first time I've written a class, so it maybe wrong.

    PHP Code:
    class SafePDO extends PDO {
     
            public static function 
    exception_handler($exception) {
                
    // Output the exception details
                
    die('Uncaught exception: '$exception->getMessage());
            }
     
            public function 
    __construct($dsn$username=''$password=''$driver_options=array()) {

                
    // Temporarily change the PHP exception handler while we . . .
                
    set_exception_handler(array(__CLASS__'exception_handler'));

                
    // . . . create a PDO object
                
    parent::__construct($dsn$username$password$driver_options);

                
    // Change the exception handler back to whatever it was before
                
    restore_exception_handler();
            }
        
    }

    class 
    SafePDO_errordisplay extends SafePDO {

        public function 
    connect_db($dsn$username=''$password=''$driver_options=array()) {
            
            
    parent::__construct($dsn$username$password$driver_options);
                try {
                
    $DB = new SafePDO($dsn$user$password$driver_options);
                }
                catch (
    PDOException $e) {
                echo 
    'Connection failed: ' $e->getMessage();
                }
             }
    }

    // Connect to the database - So my other coders only need to know the dbname to connect, not the MySQL log-in details. 
    function SafePDOPersist($dbname){
    $DB = new SafePDO_errordisplay("mysql:host=localhost;dbname=$dbname"$DBusername$DBpassword, array(PDO::ATTR_PERSISTENT => true));
    }

    function 
    SafePDOCOE($dbname){
    $DB = new SafePDO_errordisplay("mysql:host=localhost;dbname=$dbname"$DBusername$DBpassword);


  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by HDRebel88 View Post
    Would this make any sense? Building in try/catch on the connection into an class extension?
    no. because exceptions work different from errors.

    the rule-of-thumb for exception handling is: catch the exception where you can handle the problem.

    since any DB error (that leads to throwing a PDOException) essentially prevents you from displaying the DB data (in case of a SELECT), the place to handle is where you do the data/HTML output, which is usually not in the DB class.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •