-3

I have the following PHP query code:

$theirTable = $dbcon->query("SELECT * FROM dogbreed");
$theirRow = $theirTable->fetch_assoc();

I am trying to understand the correct method for displaying all records in my chosen table in a loop. My request is done like so:

            <?php
                while ($fow = $theirRow){
                    echo $fow['dogtype'];
                }
            ?> 

This code proves problematic, as it repeats the first response endlessly. So I end up getting a result like dobermandobermandobermandobermandobermancontinously. Not sure what my problem is from a logic standpoint.

Jason Chen
  • 2,487
  • 5
  • 25
  • 44

1 Answers1

1

You should iterate over fetch_assoc result

      <?php
            while ($fow = $theirTable->fetch_assoc()){
                echo $fow['dogtype'];
            }
        ?> 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • remove the semicolon at while – hungrykoala Jun 19 '17 at 11:29
  • @hungrykoala ... many thanks .. semicolon removed .. – ScaisEdge Jun 19 '17 at 11:29
  • I am having trouble identifying how your code is different from mine. yet it does not repeat the same line over again. however, this code only shows me 1 value. for some reason it does not echo the entire results of my array. I have 2 dogtypes and it is only echoing 1. – Jason Chen Jun 19 '17 at 11:34
  • The reason it is different is because it is fetching a row during each loop, where you were assigning one row to a variable and trying to loop on that. – Jay Blanchard Jun 19 '17 at 11:38
  • @JasonChen simple ... in you you assign to$fow the value form $theirTable->fetch_assoc() just once .. and you don't call no more $theirTable->fetch_assoc() .. so the value remain the same .. in mine the while loop call the $theirTable->fetch_assoc() until find result .. so i repeated fo all the rows selected ... hope is clear – ScaisEdge Jun 19 '17 at 11:38
  • I C. After I removed the `$theirRow` variable I was able to get it to work. – Jason Chen Jun 19 '17 at 11:45
  • 1
    @JasonChen explain better you comment .. i don't understand – ScaisEdge Jun 19 '17 at 11:46
  • I just meant that after I deleted my `$theirRow` variable I was able to echo both values using your code. It's strange considering that `$theirRow` wasn't used when I copied your code, and yet it only allowed my PHP to echo the 1 result. – Jason Chen Jun 19 '17 at 11:52
  • 1
    @JasonChen ... remember that each time you call $theirTable->fetch_assoc() you move the pointer to the next row .. – ScaisEdge Jun 19 '17 at 11:55