-
Posts
2,134 -
Joined
-
Last visited
-
Days Won
42
Everything posted by benanamen
-
Concat multidimensional array with normal array
benanamen replied to luiggicc's topic in PHP Coding Help
Better yet, why dont you tell us what the real problem is that you are trying to solve rather than your attempt at solving it. -
I would think that with a unique index, Mysql already knows there is only going to be one result.
-
You need to stop grabbing stuff off the net. This code is junk. Your time will be better spent to actually learn how to do what you want.
-
Your code is obsolete, insecure and has been completely removed from Php. It is also vulnerable to SQL Injection. You need to use PDO. https://phpdelusions.net/pdo
-
It's not going to work without the opening and closing PHP brackets
-
Now that you have it working it would be in your "be a better coder" interests to post your "fixed" code. I suspect that although it may work, it is still wrong. We are happy to review what you came up with.
-
PHP working differently in different browsers
benanamen replied to trenttdogg's topic in PHP Coding Help
I wasn't trying to be an ***. Programmer's (me included) can be terse in answering. The PDO tutorial link I gave you is very good. You should be able to grasp it fairly easy. We all had to start somewhere. I was basically trying to keep you from wasting your time and provide you the correct direction. -
PHP working differently in different browsers
benanamen replied to trenttdogg's topic in PHP Coding Help
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. -
PHP working differently in different browsers
benanamen replied to trenttdogg's topic in PHP Coding Help
So much wrong here. The entire code is junk. Toss it and learn PDO. https://phpdelusions.net/pdo -
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
-
You are mixing mysql, mysqli and PDO. You need to use all PDO. The mysql_* functions have been completely removed from Php. Also, remove the id from your query,
-
@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.
-
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.
-
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.
-
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);
-
The OP said enough right here In that case, @Jaques1 and I are correct.
-
He doesn't have a list. Somebody lied to him
-
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.
-
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.
-
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(); }
-
Categories and subcategories array and dynamic dropdown list
benanamen replied to z4z07's topic in PHP Coding Help
The fact that you have more than one table says you're wrong, but you know better right?