Database
Results 1 to 4 of 4

Thread: Database

  1. #1
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490

    Database

    Well apparently my database class was wrong so I've read through a few things and I've attempted to improve it now I'm getting the error message Undefined variable: dsn

    PHP Code:
    <?php
    class Database extends PDO

    {
        protected static 
    $instance;
        private 
    $dsn 'localhost';
        private 
    $username 'root';
        private 
    $password '';
        private 
    $dbname 'shops';
        
        public function 
    __construct($dsn$username NULL$password NULL$options = [])
        {
            
    $default_options = [
                
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                
    PDO::ATTR_EMULATE_PREPARES => false,
                
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            ];
            
    $options array_replace($default_options$options);
            
    parent::__construct($dsn$username$password$options);
        }
        
        public static function 
    instance()
        {
            if (
    self::$instance === null)
            {
                
    self::$instance = new self;
            }
            return 
    self::$instance;
        }

        public function 
    run($sql$args = [])
        {
            
    $stmt $this->pdo->prepare($sql);
            
    $stmt->execute($args);
            return 
    $stmt;
        }
    }
    Originally it was

    PHP Code:
    class Database extends PDO

    {
        private 
    $host 'localhost';
        private 
    $user 'root';
        private 
    $pass '';
        private 
    $dbname 'shops';

        public function 
    __construct($host null$user null$pass null$opts null)
        {
            
    parent::__construct("mysql:host={$this->host};dbname={$this->dbname}"$this->user$this->pass$opts);
            
    $this->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        }

    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  2. #2
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490
    OH never mind, it's a bad idea
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,882
    $this->dsn for an instance variable. (Repeat as needed for the other parameters.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    490
    Yeah, the example code is nonsense according to the author it's the "right way" to do it, the guy spent too much time stroking his ego to be of much us
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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