Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Posts posted by benanamen

  1. I am stuck on the following DI code with the DSN. Relevant lines 62, 77, 78.

     

    I expect line 77 to work since the output of line 62 is exactly what works in line 78. I get

     

    Fatal error: Uncaught PDOException: could not find driver in C:\Program Files (x86)\Ampps\www\dependencyinjection\DatabaseConnection.php on line 77. 

     

    If I use line 78 instead, it works.

     

    Anyone know what the problem is?

     

     

    $connection var_dump

    C:\Program Files (x86)\Ampps\www\dependencyinjection\DatabaseConnection.php:75:
    object(DatabaseConnection)[2]
      private 'configuration' => 
        object(DatabaseConfiguration)[1]
          private 'host' => string 'localhost' (length=9)
          private 'port' => int 3306
          private 'username' => string 'root' (length=4)
          private 'password' => string 'mysql' (length=5)
    
    <?php
    
    class DatabaseConfiguration
    {
        /**
         * @var string
         */
        private $host;
        /**
         * @var int
         */
        private $port;
        /**
         * @var string
         */
        private $username;
        /**
         * @var string
         */
        private $password;
        public function __construct(string $host, int $port, string $username, string $password)
        {
            $this->host = $host;
            $this->port = $port;
            $this->username = $username;
            $this->password = $password;
        }
        public function getHost(): string
        {
            return $this->host;
        }
        public function getPort(): int
        {
            return $this->port;
        }
        public function getUsername(): string
        {
            return $this->username;
        }
        public function getPassword(): string
        {
            return $this->password;
        }
    }
    
    class DatabaseConnection
    {
        /**
         * @var DatabaseConfiguration
         */
        private $configuration;
        /**
         * @param DatabaseConfiguration $config
         */
        public function __construct(DatabaseConfiguration $config)
        {
            $this->configuration = $config;
        }
        public function getDsn(): string
        {
            return sprintf(
                '"mysql:host=%s;dbname=test", %s, %s',
                $this->configuration->getHost(),
                $this->configuration->getUsername(),
                $this->configuration->getPassword()
                //$this->configuration->getPort()
            );
        }
    }
    
    $config = new DatabaseConfiguration('localhost', 3306, 'root', 'mysql');
    $connection = new DatabaseConnection($config);
    
    echo $dsn = $connection->getDsn();// "mysql:host=localhost;dbname=test", root, mysql
    
    
        $pdo = new PDO($dsn);// Doesnt Work
        //$pdo = new PDO("mysql:host=localhost;dbname=test", root, mysql); //Works
    
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    
        $sql  = "SELECT * FROM users";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
    
        $result = $stmt->fetchAll();
    
                echo '<pre>';
                print_r($result);
    
    
  2. I ran across the following example of a Factory Pattern. My question is, why would you have all this Factory code when you could just call one of the extended button classes that you need?

    <?php
    abstract class Button {
        protected $_html;
    
        public function getHtml()
        {
            return $this->_html;
        }
    }
    
    class ImageButton extends Button {
        protected $_html = "..."; //This should be whatever HTML you want for your image-based button
    }
    
    class InputButton extends Button {
        protected $_html = "..."; //This should be whatever HTML you want for your normal button (<input type="button"... />);
    }
    
    class FlashButton extends Button {
        protected $_html = "..."; //This should be whatever HTML you want for your flash-based button
    }
    
    class ButtonFactory
    {
        public static function createButton($type)
        {
            $baseClass = 'Button';
            $targetClass = ucfirst($type).$baseClass;
    
            if (class_exists($targetClass) && is_subclass_of($targetClass, $baseClass)) {
                return new $targetClass;
            } else {
                throw new Exception("The button type '$type' is not recognized.");
            }
        }
    }
    
    $buttons = array('image','input','flash');
    foreach($buttons as $b) {
        echo ButtonFactory::createButton($b)->getHtml();
    } 
  3. From the Forums Rules

     

    Users will not post their homework questions expecting to get their homework coded for them. If you have a question about part of the assignment that you do not understand, please post asking for an explanation rather than the code.

     

  4. This thread reminds me of the people who spend days trying to figure out how to fill gaps in numeric IDs and end up with hideously complex, bug-ridden locking gymnastics – so that they can have a “beautiful” sequence which nobody cares about.

     

    LOL! While I never did anything about it, in my early days missing numbers really bothered me.

     

    Notion, in a good way.

     

    An intervention is the process by which an addict's family, friends, counselors or professional intervention specialists can show the addict his destructive behaviors in a way that may result in the addict choosing to seek help immediately.

     

    We here need to gather to save you from yourself.  :happy-04: 

    • Like 1
  5. Without the exact detail on the public_id I cannot make any comments on that part.

     

    they also share some common properties and as such a supertype/subtype solution is appropriate.

     

    You are on the right track here. Is this the part you do not understand how to do? On a side note, MongoDB is very well suited to this type of data.

  6. This is going to be a reeeelly long thread. OP, the problem is you don't know what you don't know. You know enough about using a saw to cut your fingers off and that makes you dangerous in the coding world. You are not going to have any luck trying to argue what you think you know with people that actually do know what they are talking about.

     

    With a tool like this I can have them add, edit, and manage connections and queries for multiple web server paths without the need to go into code.

     

    If you just needed to get the job done, you could have saved the company and yourself even more time and money if you just used the free Mysql Workbench or Phpmyadmin. Aside from that, there are numerous other tools that do the same thing, free and paid. You are trying to re-invent the wheel instead of getting the job done.

     

    If you really want to be a programer, listen carefully to what we tell you and apply it and get used to harsh criticism. There is a whole lot you don't know.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.