如何仅显示发送到数据库的最后一篇文章?PHPMYADMIN

所以我使用 foreach 循环从 phpmyadmin 中的数据库中获取数据,然后将其显示在我的 indextest.php 文件中,该文件具有以下代码:


<?php foreach ($posts as $post): ?>


    <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 mt-4">


        <h6>

        <span style="color: blue;">BREAKING NEWS</span>

        <span id="test" style="padding-left: 10px;"><?php echo date('F j, Y', strtotime($post['created_at'])); ?></span>

        </h6>

        <!-- HEADLINE -->

        <h1><strong><a href="/single_page_main.php?id=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a></strong></h1>

            

        </strong></h1></a>

        <!-- TEXT BODY -->

        <h4><?php echo $post['body']; ?></h4> <!--displays the full body -->

        <h4><?php echo html_entity_decode(substr($post['body'], 0, 150) . '...'); ?></h4> 


        <a href="/single_page_main.php">

        <div class="button_subs float-left mb-4">Read More</div></a>


        </div>


        <!-- IMAGE -->

        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

        <img src="<?php echo '/assets/images/' , $post['image']; ?>" style="width: 100%;">


    </div>


<?php endforeach; ?>

问题是它抓取数据库中的所有帖子,但是我只想抓取发送到数据库的最后一篇帖子(最后一行)?这可能吗?谢谢


天涯尽头无女友
浏览 79回答 1
1回答

白猪掌柜的

使用需要此 SQL 查询id DESC LIMIT 1:-SELECT&nbsp;*&nbsp;FROM&nbsp;TABLE&nbsp;t&nbsp;ORDER&nbsp;BY&nbsp;t.id&nbsp;DESC&nbsp;LIMIT&nbsp;1
打开App,查看更多内容
随时随地看视频慕课网APP