ashland3000 ashland3000 - 7 months ago 15
SQL Question

How to select ALL usernames from my members table?

I am trying to figure out how to select and display all the usernames stored in the members table in my MySQL database.

This is my current code:

$stmt = $db->prepare("SELECT username FROM members");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

echo $row['username'];


This only lists one username though.

How would I go about getting and listing all usernames?

Thank you in advance!

Answer

Try this

$stmt = $db->prepare("SELECT username FROM members");
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    echo $row['username'];
}
Comments