Jump to content

Perpared Statements and Printing a Column on Screen?


glassfish

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?

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.