Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BetterPDO extends PDO
- {
- private $realPDO = NULL;
- private $dsn = "";
- private $username = "";
- private $password = "";
- private $options = [];
- public function __construct ($dsn, $username = "", $password = "", $options = [])
- {
- $this -> dsn = $dsn;
- $this -> username = $username;
- $this -> password = $password;
- $this -> options = $options;
- }
- private function getRealPDO ()
- {
- if (is_null ($this -> realPDO))
- {
- $this -> realPDO = new PDO ($this -> dsn, $this -> username, $this -> password, $this -> options);
- }
- return $this -> realPDO;
- }
- // We're only implementing exec for brevity but you have to do this for all public methods of PDO
- public function exec ($sql)
- {
- $retries = 0;
- while (true)
- {
- try
- {
- return $this -> getRealPDO () -> exec ($sql);
- }
- catch (PDOException $ex)
- {
- $this -> realPDO = NULL;
- if (++$retries > 5)
- {
- // We've passed our retry limit
- throw $ex;
- }
- }
- }
- }
- }
- /* Your Database Name */
- $dbname = 'mydatabase';
- /* Your Database User Name and Passowrd */
- $username = 'root';
- $password = 'password';
- try {
- /* Establish the database connection */
- $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- /* your code goes here*/
- } catch(PDOException $e) {
- echo 'ERROR: ' . $e->getMessage();
- }
- //mysql_close($conn);
- $conn=null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement