Liam
Liam

Reputation: 9855

Return data from MySQL table with PHP/PDO

I'm new to PHP and very new to PDO. Everyone's been telling me to learn.

I've manage to establish a connection to my database, and I'm trying to return my results and wrap each row in a DIV, but it's only returning an array with all the column titles.

I currently have:

$sql = "SELECT * FROM users";
$q   = $conn->query($sql) or die("failed!");
while($r = $q->fetchAll(PDO::FETCH_ASSOC)){
    print_r($r);
}

Can anybody point me in the right direction or give me some useful sites that can help?

Upvotes: 0

Views: 3909

Answers (2)

Michael Westblade
Michael Westblade

Reputation: 61

$stmt = $conn->prepare("SELECT * FROM users");

$stmt->execute();

foreach( $stmt as $row )
{
    echo "<div>" . $row['column'] . "</div>"
}

Upvotes: 2

Liam
Liam

Reputation: 9855

It was easier for me to do this...

while($r = $q->fetch(PDO::FETCH_LAZY)){

    echo '<div>';
    echo $r['id'];
    echo $r['first_name'];
    echo $r['surname'];
    echo $r['hometown'];
    echo $r['facebook'];
    echo '</div>';

}

Upvotes: 0

Related Questions