Jump to content

Perpared Statements and Printing a Column on Screen?


glassfish
 Share

Recommended Posts

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
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.