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

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

Html <br> tag - <br>: The Line Break element - HTML: HyperText Markup Language | MDN

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…

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

$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

1 Like

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

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {			
    echo '<br>';
	echo implode(', ', $row);
    echo '<br>';
}
Sponsor our Newsletter | Privacy Policy | Terms of Service