0

I have a db table that has two columns, id and category. my aim is to use a while loop to echo out the category from the db into a li and an anchor.

Here's the code:

<ul class="nav navbar-nav">
                    <?php $connect = new db();
                          $stmt = $connect->link->query("SELECT * FROM categories")->fetchall();
                          while($row = $stmt){
                            $category = $row["category"];
                            echo "<li><a href='#'>{$category}</a></li>";
                          }

                    ?>
                </ul>

i am getting an error but it doesnt make sense to me. its saying undefined index "category". its referring to where i assigned $category to the $row[].

i know for sure that the data is being pulled because i used the print_r() function and it printed them out

edit: as requested the output from a var_dump()

C:\wamp64\www\cms\CMS_TEMPLATE\includes\nav.php:21:
array (size=4)
  0 => 
    array (size=4)
      'category_id' => string '1' (length=1)
      0 => string '1' (length=1)
      'category' => string 'bootstrap' (length=9)
      1 => string 'bootstrap' (length=9)
  1 => 
    array (size=4)
      'category_id' => string '2' (length=1)
      0 => string '2' (length=1)
      'category' => string 'javascript' (length=10)
      1 => string 'javascript' (length=10)
  2 => 
    array (size=4)
      'category_id' => string '3' (length=1)
      0 => string '3' (length=1)
      'category' => string 'html' (length=4)
      1 => string 'html' (length=4)
  3 => 
    array (size=4)
      'category_id' => string '4' (length=1)
      0 => string '4' (length=1)
      'category' => string 'css' (length=3)
      1 => string 'css' (length=3)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cole-p1
  • 1
  • 1

1 Answers1

0

$stmt is a list of all of the rows in the database - you use ->fetchall(); to get the results of the query - this means it is a 2 dimensional array with rows indexed by numerics and each row having the field names. So you would be better of replacing

while($row = $stmt){

with something that would loop over the results...

foreach ( $stmt as $row ) {

If you need to use a while loop, then you can remove the fetchAll and use the while loop to fetch each row one at a time with fetch()...

$stmt = $connect->link->query("SELECT * FROM categories");
while($row = $stmt->fetch()){
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • i wanted to avoid using a foreach loop as it apparently takes up more space in memory. plus i need the practice with while loops and pdo. does anyone have a solution regarding the while loop? – cole-p1 Oct 26 '19 at 10:22
  • I've updated with a while loop, but the issue with `foreach()` taking more memory is not worth worrying about. – Nigel Ren Oct 26 '19 at 10:25
  • Thanks @nigel Ren. it was driving me nuts – cole-p1 Oct 26 '19 at 10:29
  • If this has helped, please consider marking it as answered - https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Nigel Ren Oct 29 '19 at 07:41