Jump to content

Echo Database Info with PHP PDO


deadendstreet

Recommended Posts

Hello

 

I'm trying to make my php more secure with PDO. Below is the sample code I have for the "view" page. It works except for two things.

 

1. It only echo's one of the submissions in the database (there are currently 7). 

2. I'd rather post the information in a table. before I updated it, I had something

echo "<h2>" . $row['program'] . "</h2>"; 
 
and so one. But I when I try to do something similar here, it screws it up. Here's my PHP any ideas?
<?php
/*** mysql hostname ***/
$hostname = 'localhost';

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

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

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=teamcontent", $username, $password);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database<br />';

    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM calendar ORDER BY airdate";

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

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

    /*** loop over the object directly ***/
    foreach($result as $key=>$val)
    {
    echo $key.' - '.$val.'<br />';
    }

    /*** close the database connection ***/
    $dbh = null;
}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

Link to comment
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.