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
https://forums.phpfreaks.com/topic/287924-echo-database-info-with-php-pdo/
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.