JoshEir Posted December 30, 2020 Share Posted December 30, 2020 Google Chrome Developer Tool causes an error when I run the following and there are not any records. I keep getting errors when I try to change it. <?php $host = 'localhost'; $user = 'root'; $pass = ''; $database = 'ecommerce'; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ); $keyword = $_GET['keyword']; $dbo = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options); $q1 = "SELECT * FROM products INNER JOIN keywords on keywords.keywordID = products.KeywordID and keywords.KeyWord1 = \"$keyword\" "; $counter = 10; $counter1 = 0; foreach ($dbo->query($q1) as $row) { Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/ Share on other sites More sharing options...
benanamen Posted December 30, 2020 Share Posted December 30, 2020 You might want to tell us exactly what that error is. We cant see your screen and you posted incomplete code. What you did post is wide open to an SQL Injection Attack. You need to use Prepared Statements. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583525 Share on other sites More sharing options...
JoshEir Posted December 30, 2020 Author Share Posted December 30, 2020 (edited) Oh, thanks, could you recommend what way to go that has plenty of instructions. For example, I was using PDO. Edited December 30, 2020 by JoshEir Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583526 Share on other sites More sharing options...
maxxd Posted December 30, 2020 Share Posted December 30, 2020 So, as @benanamen pointed out above, the code you posted is incomplete and "I keep getting errors when I try to change it" isn't nearly specific enough to help us to help you debug. In addition, Google Chrome developer tools won't cause errors - they will report them (depending on the error type); depending on which tab you're on, those errors may be server-side (php) or they may be client-side(JavaScript). You'll have to tell us what the error is and which tab in the tools is reporting it. As far as the comment about prepared statements, again benanamen is correct - check out these links: https://www.php.net/manual/en/pdo.prepared-statements.php https://www.php.net/manual/en/pdo.prepare.php 1 Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583527 Share on other sites More sharing options...
JoshEir Posted December 30, 2020 Author Share Posted December 30, 2020 Thanks for the links, I'll look at them next. This is what happened. I was quite mad from a hard to find bug with Visual Code. Something else happened with it that convinced me the only way to be is to download code that has cost. So, I was working with Sublime editor and Wamp server when the error, in question, was found. Now the reason for the the unfinished code I submitted was because I thought the foreach was failing because there was no record and what I was looking for was a way to count the rows and break if there weren't any records. I couldn't find the bug, so I decided to use Visual Code again because I was having just as much problems using Sublime editor. Low and behold, I found the bug quickly, and you were right that the error would help, because it did! When there was no record it was throwing an exception because my string of html data was undefined because it was in the foreach. So the moral of the story is, bugs really do help you find problems, Visual Code is fine, and it is fine to use free software. Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583546 Share on other sites More sharing options...
Strider64 Posted December 31, 2020 Share Posted December 31, 2020 4 hours ago, JoshEir said: Thanks for the links, I'll look at them next. This is what happened. I was quite mad from a hard to find bug with Visual Code. Something else happened with it that convinced me the only way to be is to download code that has cost. So, I was working with Sublime editor and Wamp server when the error, in question, was found. Now the reason for the the unfinished code I submitted was because I thought the foreach was failing because there was no record and what I was looking for was a way to count the rows and break if there weren't any records. I couldn't find the bug, so I decided to use Visual Code again because I was having just as much problems using Sublime editor. Low and behold, I found the bug quickly, and you were right that the error would help, because it did! When there was no record it was throwing an exception because my string of html data was undefined because it was in the foreach. So the moral of the story is, bugs really do help you find problems, Visual Code is fine, and it is fine to use free software. Well, I find syntax bugs to be a pain when you have logic errors on top of them. You fix all the syntax bugs and you code doesn't work the way you expect it to then you feel like you just wasted a couple hours of work, plus you feel like banging you head against the desk. The best way to learn PHP in my opinion is to learn the code syntax properly by going to php.net and following tutorials. An IDE is just away to debug syntax errors more quickly in order for you to concentrate on the logical errors. That's my opinon. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583552 Share on other sites More sharing options...
JoshEir Posted December 31, 2020 Author Share Posted December 31, 2020 Tutorials sound smart. Someone said PHP takes 6 months to learn. This is what helped me do the prepare with PDO : https://phpdelusions.net/pdo. I got it working pretty quick. Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583553 Share on other sites More sharing options...
maxxd Posted December 31, 2020 Share Posted December 31, 2020 13 minutes ago, JoshEir said: Someone said PHP takes 6 months to learn. I've been coding PHP for about 20 years now - version 4 was just about to come out when I started. Most of that time was professional (I was lucky enough to have a job that paid me to learn while I worked at first). To this day, I learn something new every day, either while taking on a problem at work or coming here. Don't expect to be able to put a timer on when you "know" php. Get proficient with the language but more importantly understand programming. If you've committed to coding, read "The Pragmatic Programmer" by Andy Hunt and Dave Thomas, and "Design Patterns" by the Gang of Four. For a php specific viewpoint, check out "PHP Objects, Patterns, and Practices" by Matt Zandstra. There are other resources I'm sure more will chime in with, but those are three that I go back to regularly. There are online sources that are very good, but there are also online resources that are very bad, and it's often difficult to tell which is which. While you're doing tutorials, check in here - this is a great place to get good advice from seasoned professionals. Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583554 Share on other sites More sharing options...
guymclarenza Posted January 7, 2021 Share Posted January 7, 2021 On 12/31/2020 at 5:31 AM, maxxd said: I've been coding PHP for about 20 years now - version 4 was just about to come out when I started. Most of that time was professional (I was lucky enough to have a job that paid me to learn while I worked at first). To this day, I learn something new every day, either while taking on a problem at work or coming here. Don't expect to be able to put a timer on when you "know" php. Get proficient with the language but more importantly understand programming. If you've committed to coding, read "The Pragmatic Programmer" by Andy Hunt and Dave Thomas, and "Design Patterns" by the Gang of Four. For a php specific viewpoint, check out "PHP Objects, Patterns, and Practices" by Matt Zandstra. There are other resources I'm sure more will chime in with, but those are three that I go back to regularly. There are online sources that are very good, but there are also online resources that are very bad, and it's often difficult to tell which is which. While you're doing tutorials, check in here - this is a great place to get good advice from seasoned professionals. I have been playing with it on and off for almost 20 years and I still need to learn new stuff every time I try and do something. I am doing silly courses like building a search engine just to see what the code does. Quote Link to comment https://forums.phpfreaks.com/topic/311949-php-problem-when-no-records-from-database/#findComment-1583697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.