I am trying to convert an SQL statement to PDO.
I cant seem to figure this out.
Here is the sql statement.
<h3>Recent Post</h3>
<?php
$sql = mysql_query("SELECT * FROM blogData ORDER BY id DESC");
while($row = mysql_fetch_array($sql)){
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
?>
<div class="features">
<div class="box"><img src="Developer/common-files/icons/time@2x.png" width="100" height="100" alt="">
<h6><?php echo $category; ?> - <?php echo $title; ?></h6>
<p><?php echo $content; ?></p>
</div>
</div>
<?php
}
?>
</div>
From what I have read I was under the impression that this would work
PDO statement
<h3>Recent Post</h3>
<?php
$stmt = $con->query('SELECT * FROM blogData ORDER BY id DESC');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
}
?>
<div class="features">
<div class="box"><img src="Developer/common-files/icons/time@2x.png" width="100" height="100" alt="Wuno Inc.">
<h6><?php echo $row['category']; ?> - <?php echo $row['title']; ?></h6>
<p><?php echo $row['content']; ?></p>
</div>
</div>
</div>
On my page I do not get any error but no information populates.
You're ending your while
statement immediately with a stray closing bracket }
. Move the bracket below your output, like you have in your first code example.
Also, don't forget to use htmlspecialchars()
around any data used in the context of HTML, to ensure you generate valid HTML and prevent injection of scripts and what not.