i need to convert this mysql into pdo i tried the following but i guess its wrong as i get no results showing. yes it may be easy for one of you but pdo is news to me so appreciate your help :)
actual code
$rowperpage = 3;
// counting total number of posts
$allcount_query = "SELECT count(*) as allcount FROM posts";
$allcount_result = mysql_query($allcount_query);
$allcount_fetch = mysql_fetch_array($allcount_result);
$allcount = $allcount_fetch['allcount'];
// select first 3 posts
$query = "select * from posts order by id asc limit 0,$rowperpage ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$shortcontent = substr($content, 0, 160)."...";
$link = $row['link'];
?>
<!-- Post -->
<div class="post" id="post_<?php echo $id; ?>">
<h1><?php echo $title; ?></h1>
<p>
<?php echo $shortcontent; ?>
</p>
<a href="<?php echo $link; ?>" class="more" target="_blank">More</a>
</div>
<?php
}
?>
what i tried
$rowperpage = 3;
// counting total number of posts
//$allcount_query = "SELECT count(*) as allcount FROM posts";
//$allcount_result = mysql_query($allcount_query);
$query = "SELECT count(*) FROM posts";
$stmt = $db->prepare($query);
$allcount_fetch = $stmt->fetch(PDO::FETCH_ASSOC);
$allcount = $stmt->fetchColumn();
// select first 3 posts
//$query = "select * from posts order by id asc limit 0,$rowperpage ";
//$result = mysql_query($query);
$qry = "select * from posts order by id asc limit 0,$rowperpage ";
$stm = $db->prepare($qry);
while($row = $stm->fetch(PDO::FETCH_ASSOC)){
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$shortcontent = substr($content, 0, 160)."...";
$link = $row['link'];
can someone show the right way to do it?