deadendstreet Posted April 22, 2014 Share Posted April 22, 2014 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(); } Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 22, 2014 Share Posted April 22, 2014 I'm not familiar with PDO, so please verify if: $result = $stmt->fetch(PDO::FETCH_ASSOC); returns the entire recordset, or just one record (where the database recordset pointer is currently pointing). Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 22, 2014 Share Posted April 22, 2014 You need to put your fetch into a loop and then use the foreach to echo each column. while ($row = $dbh->fetch(PDO::FETCH_ASSOC)) { foreach ($row as $k=>$v) echo ...... } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.