Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Everything 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. The OP said enough right here In that case, @Jaques1 and I are correct.
  3. What @Jaques1 showed you is the way to do it. You are wanting to know if there are results returned or not, true or false. How many are returned if any is irrelevant.
  4. It doesn't matter. The code has been completely removed from Php. Don't use it. At the least, you should be using Mysqli
  5. You cannot just through an i on the end of the deprecated functions. Take some time and read the manual. You should be using PDO anyways. https://phpdelusions.net/pdo
  6. You need to create a unique constraint on whatever identifies the users. They should not be allowed to sign up more than once with the same parameters.
  7. 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(); }
  8. The fact that you have more than one table says you're wrong, but you know better right?
  9. You can start with a proper database design. There is no such thing as sub categories. They are ALL categories. Some are Parents, some are Children. Look this over. http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
  10. The Bike Shed Problem http://it.toolbox.com/blogs/coneblog/about-project-management-the-bikeshed-problem-32923
  11. Suddenly? I have never asked for homework help and I don't violate any of the other rules. What are you trying to say?
  12. From the Forums Rules
  13. Post the relevant code. We are not going to download an unknown zip file.
  14. LOL! While I never did anything about it, in my early days missing numbers really bothered me. Notion, in a good way. We here need to gather to save you from yourself.
  15. @NotionCommotion, I think it's time for an intervention.
  16. I don't have time at the moment to get into the implementation. I am sure Barand will jump in before I can. As far as MyISAM, what is stopping you from using InnoDB?
  17. Without the exact detail on the public_id I cannot make any comments on that part. 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.
  18. Isn't this when you would use a destructor in the class?
  19. Why are you creating multiple tables? You should have ONE animals table linked to an animal_type_id in a animal type table. What is with the multiple keys?
  20. Your code is obsolete and vulnerable to exploits and has been completely removed from Php. You need to use PDO with prepared statements. https://phpdelusions.net/pdo
  21. Your approach is wrong in the first place. You never ever put variables in the query. You need to use prepared statements and PDO. https://phpdelusions.net/pdo Also, if you have 42 columns in a single table it is highly likely your DB design is wrong. A DB is not a spreadsheet. Look up and learn Database normalization.
  22. Is that the best you can do? What basics? What Month? What number? What Array?
  23. You mean something like this? https://css-tricks.com/examples/DynamicOrderForm/
  24. 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. 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.