# How do I display results from a while loop each on its own line in PDO?

**URL:** <https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572>\
**Category:** General PHP Help\
**Created:** [July 27, 2023, 9:06am UTC](https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572 "2023-07-27T09:06:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 27, 2023, 9:06am UTC](https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572/1 "2023-07-27T09:06:54Z")

</div>

I know this is probably super simple, but even Bing A.I. couldn’t figure it out. lol Basically, I need all the results from this query on separate lines. The code below works, but currently makes it a big mess and one after the other like a giant paragraph. I’m omitting the DB strings since that works, and it is correctly accessing the referenced tables.

Thanks!

```
$stmt = $pdo->prepare('SELECT email FROM odh_old WHERE email NOT IN (SELECT email FROM blood)');
$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
	echo $row['email'] . "\n";
}
print_r($row);
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [July 27, 2023, 9:11am UTC](https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572/2 "2023-07-27T09:11:50Z")

</div>

Html `<br>` tag - [\<br\>: The Line Break element - HTML: HyperText Markup Language | MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br)

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 27, 2023, 10:25am UTC](https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572/3 "2023-07-27T10:25:42Z")

</div>

Okay, getting there. I did this:

```
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {    
   	echo '<br>';
   	print_r($row);
   	echo '<br>';  
}

```

Which works, but appends every entry with: Array ( [email] =\> $var )

Hmmm…stumped…

---

<div class="post-metadata">

**Author:** ![katsry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@katsry](https://www.phphelp.com/u/katsry)\
**Post date:** [July 28, 2023, 12:56pm UTC](https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572/4 "2023-07-28T12:56:48Z")

</div>

Your code is almost correct. You need to echo out the HTML break tag (`<br>` ) instead of `"\n"`. Try this code

```auto
$stmt = $pdo->prepare('SELECT email FROM odh_old WHERE email NOT IN (SELECT email FROM blood)');
$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
	echo htmlspecialchars($row['email']) . "<br>";
}

```

Good luck

---

<div class="post-metadata">

**Author:** ![zoldos](https://www.phphelp.com/user_avatar/www.phphelp.com/zoldos/32/4221_2.png) [@zoldos](https://www.phphelp.com/u/zoldos)\
**Post date:** [July 28, 2023, 7:00pm UTC](https://www.phphelp.com/t/how-do-i-display-results-from-a-while-loop-each-on-its-own-line-in-pdo/35572/5 "2023-07-28T19:00:38Z")

</div>

Thanks! I actually did this, and the output is perfect:

```
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {			
    echo '<br>';
	echo implode(', ', $row);
    echo '<br>';
}
```
