Reputation: 23
I am trying to print my result data returned from the fetchAll function. How can i bind the name and desc variable to the returned results.
<?php
$get = $db->prepare("SELECT * FROM Products ORDER BY DateAdded DESC LIMIT 4");
$get->execute();
$results = $get ->fetchAll();
foreach($results as $result){ ?>
$name = $result['Name'];
$desc= $result['Desc'];
<h4><?php echo $name ?></h4>
<h4><?php echo $desc?></h4>
<?php
}
?>
Upvotes: 1
Views: 35
Reputation: 7661
Try the following:
$results = $get->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $key => $value){ ?>
echo '<h4>'.$value['name'].'</h4>
<h4>'.$value['desc'].'</h4>';
}
You also had a space between $get
and ->fetchAll()
You might want to remove the php closing tag ?>
as it might give you problems when including.
Upvotes: 1
Reputation: 9765
You don't have to fetch all results at once to do that. It's better to use fetch
method for that task.
while ($row = $get->fetch(PDO::FETCH_ASSOC)) {
echo $row['Name'];
}
And about main problem here, you are missing closing tag after $result['Desc'];
Upvotes: 0