【发布时间】:2018-01-22 02:48:23
【问题描述】:
我需要将此 mysql 转换为 pdo 我尝试了以下操作,但我猜它是错误的,因为我没有显示任何结果。是的,这对你们中的一个人来说可能很容易,但 pdo 对我来说是个新闻,所以感谢你的帮助:)
实际代码
$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
}
?>
我尝试了什么
$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'];
有人可以展示正确的方法吗?
【问题讨论】:
-
从这里开始你的 PDO php.net/manual/en/book.pdo.php
-
做一些适当的调试,找出为什么你修改的脚本不能自己工作。这包括阅读使用 PDO 时常用的调试方法。