glassfish Posted November 25, 2014 Share Posted November 25, 2014 The Connection: <?php $dsn = "mysql:dbname=phpblog;host=localhost"; $user = "root"; $password = ""; try{ $pdo = new PDO($dsn, $user, $password); }catch(PDOException $e){ echo "Connection failed: " . $e->getMessage(); } ?> The Script: <?php $query = $pdo->prepare("SELECT * FROM posts WHERE id = ?"); $query->bindValue("1", $_GET['post_id']); $query->execute(); ?> I want to print the column "headline" on screen. I know how to do it with a foreach loop: Example: foreach($query as $data){ echo $data['headline']; } What can I do there? Link to comment https://forums.phpfreaks.com/topic/292709-perpared-statements-and-printing-a-column-on-screen/ Share on other sites More sharing options...
ginerjm Posted November 25, 2014 Share Posted November 25, 2014 while ($row = $query->fetch(PDO::FETCH_ASSOC)) { echo $row['headline'],'<br>'; } Also - you shouldn't do a "select *' query when all you really want is just the one column. Link to comment https://forums.phpfreaks.com/topic/292709-perpared-statements-and-printing-a-column-on-screen/#findComment-1497658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.