DroiDar DroiDar - 7 months ago 11
SQL Question

PHP Prepared Statements: Echo the Result

i want to Display the Result ('Positio: 2' or 'Position: 1') via a Echo
but $statement is a Object of class PDOStatement and not a String, how do i get it to see only the result of $statement? Thank you

<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb1', 'root', '');

$idV = $_GET['id'];

$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = ?");
$statement->execute(array($idV));

echo "Position: //$result_Of_Statement\\ ";
?>

Answer

Here's how I would do it:

$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = :idV");
$statement->bindParam(':idV', $idV);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{
  echo $row['position'];
}

You could also do away with the while loop if you wanted to.