Jump to content

Echo all results from PDO query


lostprophetpunk

Recommended Posts

Hello there, I am just trying out PDO in php.

 

However, I have come across a problem. How would I echo out all of the data that is in the table? The code below only echos out the first entry in the database.

 

<?php
// username and password taken out of code

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = '';

/*** mysql password ***/
$password = '';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=blogv2", $username, $password);
    /*** connected ***/
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }


    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM posts";

    /*** fetch into an PDOStatement object ***/
    $stmt = $dbh->query($sql);

    /*** echo number of columns ***/
    $obj = $stmt->fetch(PDO::FETCH_OBJ);

    /*** loop over the object directly ***/
    echo $obj->title.'<br />';
    echo $obj->entry.'<br />';
    echo $obj->poster;

?>

 

If anyone could help out, that would be great as I am new to PDO.

Link to comment
https://forums.phpfreaks.com/topic/174086-echo-all-results-from-pdo-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.