我在3周前开始进行网络开发。今天我遇到了这个问题,只有第一次($ query-> fetch())有效,第二次没有。
我四处搜索,我应该使用$ rows数组,但我想我实现错误,因为它没有工作。
<div class="container-fluid">
<div class="row">
<aside class="sidebar col-md-3 col-md-offset-1 col-md-push-8">
<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php while($query->fetch()): ?>
<li><?php echo "<a href='post.php?
id=$post_id'>$title</a>" ?></li>
<?php endwhile ?>
</ul>
</div>
</aside>
<div class="col-md-8 posts col-md-pull-4">
<div class="row">
<?php
while($query->fetch()):
$lastspace = strrpos($content, ' ');
?>
<article class="post-excerpt">
<div class="col-md-6 post">
<a href="#">
<img src="../img/blog_posts/<?php echo $image ?>" class="img-responsive">
</a>
<header>
<a href="#">
<h3><?php echo $title ?></h3>
</a>
<p class="post-meta">
<?php echo $category ?>
<span><?php echo $date ?></span>
</p>
</header>
<p>
<?php echo substr($content, 0, $lastspace)."<a href='post.php?id=$post_id'> ..Read More</a>" ?>
</p>
</article>
<?php endwhile ?>
</div>
</div>
<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev'>Prev</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next'>Next</a>";
}
?>
</div>
</div>
</div>
查询:
$query = $db->prepare("SELECT id, title, date, image, LEFT(content,500) AS content, category FROM blog INNER JOIN categories ON categories.category_id=blog.category_id order by id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $date, $image, $content, $category);
答案 0 :(得分:0)
如果要从结果集中提取数据两次意味着您需要在第二个while循环之前使用$query->data_seek(0);
将内部数据指针重置为0。否则你会收到错误。
更新1:在第二次循环之前使用此
$query->data_seek(0);
答案 1 :(得分:0)
我通过在第一个循环中使用数组然后在for循环中回显它来解决它,其中第二个while循环是。
<div class="container-fluid">
<div class="row">
<div class="col-md-8 posts">
<div class="row">
<?php
while($query->fetch()):
$lastspace = strrpos($content, ' ');
$link[] = "<li><a href='post.php?id=$post_id'>$title</a></li>";
?>
<article class="post-excerpt">
<div class="col-md-6 post">
<a href="#">
<img src="../img/blog_posts/<?php echo $image ?>" class="img-responsive">
</a>
<header>
<a href="#">
<h3><?php echo $title ?></h3>
</a>
<p class="post-meta">
<?php echo $category ?>
<span><?php echo $date ?></span>
</p>
</header>
<p>
<?php echo substr($content, 0, $lastspace)."<a href='post.php?id=$post_id'> ..Read More</a>" ?>
</p>
</div>
</article>
<?php endwhile ?>
</div>
<div class="col-md-6 pagination">
<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev' id='previous'>Prev</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next' id='next'>Next</a>";
}
?>
</div>
</div>
<aside class="sidebar col-md-3">
<div class="widget">
<h2>About me</h2>
<p>
I'm Yousef. Aspiring web, mobile, and soon-to-be game developer. Well I'm also a geek, a nerd, and I love comic books and comic book accesories. One day I will make the world's coolest website.
</p>
</div>
<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php
for ($x = 0; $x <= 2; $x++) {
echo $link[$x];
}
?>
</ul>
</div>
</aside>
</div>