Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.