Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Posts posted by benanamen

  1. Side note: since there should only be one entry with any given username, you could optimize the query using the LIMIT clause.

    $stmt = $dbh->prepare("SELECT * FROM administrator WHERE korisnicko_ime = :username LIMIT 1");

    That way MySQL stops looking for more records as soon as it finds the first match.

     

     

    I would think that with a unique index, Mysql already knows there is only going to be one result.

  2. You should have asked your 13 year old. He would have told you that your code is obsolete and has been completely removed from PHP and that your "code" is full of security vulnerabilities. PDO is not a new language. If you took a second to click on the link I provided you would have seen that. You are never going to be a programmer if you get butt hurt when someone tells you the truth.

  3. The top is PDO. The bottom is mixed mysql and mysqli. The mysql functions were deprecated over 11 years ago and completely removed in php 7. Do not use Mysql_* no matter what version Php you have. You should be running no less than Php 5.6 at this point.

     

    Never ever put variables in your query. You need to use prepared statements. Study this PDO tutorial https://phpdelusions.net/pdo

  4. @Jaques1, thanks for the reminder about PDO. So easy to forget it is actually a Class.

     

    Getting back to what I am actually doing as mentioned in other threads, my focus is converting the following minimal login/registration procedural app on my repo to OOP.

     

    https://github.com/benanamen/perfect_app

     

    This project's sole purpose is for practice and learning OOP. Given what is seen in the repo, where is the first place to start? I assumed it would be database.php or public/registration.php, thus the subjects of my last few threads.

     

    This project already contains numerous code recommendations from @Jaques1 from over the last couple years as it has proven out to be the proper way to code. I have pretty much tapped out on procedural improvements so I decide to fork and use it to learn OOP. I have already gone the TWIG templating route although it is not reflected in this repo. I didn't want to clutter the space while learning OOP.

     

    Any OOP suggestions or recommendations about anything in the repo are welcome. Like most of you, I am just trying to be a better programmer.

  5. The whole chapter is bad. This doesn't teach anything about dependency injection.

     

     

    See, that's the problem I keep running into. I don't know enough yet to know if something is wrong. So now, I just spent the last several days trying to learn bad code. I am about ready to just give up on OOP. I have been coding for over 25 years with procedural and have not had any problems with it and any programmer with basic knowledge could jump right in and work on it. I code alone, so any "working on a team" considerations doesnt even matter.

     

    Which site is the bad one? Tutorialpoint or the other one?

     

    As far as my own projects, I mainly do backend DB apps so starting at the beginning I would need to know how to properly make the DB connection and from there make a query and output the data, let's say starting with a select and tabular records list. I already know to code against an Interface so I can use various DB types. I have read tons of OOP code that does a DB connection. Problem is, I don't know what is right and wrong.

  6. Except for the DSN string, I didn't write the code. It came from this site that is supposed to be teaching patterns http://designpatternsphp.readthedocs.io/en/latest/Structural/DependencyInjection/README.html

     

    From what you see on their page, is what they show wrong? Their example of DI was incomplete lacking the actual DSN part so I had to wing it to see what it did so I could understand it.

     

    As far as class diagrams, I have spent many days studying this site teaching patterns using Java that shows a diagram for every Pattern  https://www.tutorialspoint.com/design_pattern/index.htm

     

    One pattern diagram they use makes sense, the builder pattern. Not sure how much is the pattern itself, or the real life example of a Fast food meal. All these shapes and animal examples everywhere just aren't cutting it for me. Is this diagram correct? (Also attached to this post) https://www.tutorialspoint.com/design_pattern/builder_pattern.htm

     

    What I need to see is a complete, proper working example, otherwise I am just guessing at how it should be done. It is helpful to hear what is wrong with my attempts but seeing how something should be done would go a lot farther.

     

    post-179806-0-73916100-1488127324_thumb.jpg

     

     

     

  7. 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);
    
    
  8. 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();
    } 
×
×
  • 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.