Jump to content

PHP problem when no records from database


Recommended Posts

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) {
  
  

 

Link to post
Share on other sites

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

  • Like 1
Link to post
Share on other sites

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.  :)

 

Link to post
Share on other sites
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.

  • Like 1
Link to post
Share on other sites
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.

Link to post
Share on other sites
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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.