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? Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.