How can I select the last 3 IDs from SQL with PHP?

-1

I am creating a kind of portal with news which is divided and the container, which is where the news is stored and the last 3 are displayed. I have this code, which shows me from id 3 to 1 but I am looking for see only the last 3, can you explain?.

Next I show the code that I have:

home_container.php

<?php
include_once('includes/conectar.php');
    $link=conectar();
?>
    <div class="container">
        <div class="row">
            <?php
                $total=mysqli_query($link,"SELECT MAX(id) FROM tbl_noticias_blog"); 
                for($total=3; $total > 0  ; $total--){
                    $obtenertitulo=mysqli_query($link,"Select * from tbl_noticias_blog order By id='$total' DESC");
                    $titulo=mysqli_fetch_array($obtenertitulo);
                        $dc=mysqli_query($link,"Select * from tblnoticias_blog where titulo='$titulo[titulo]' Order By id DESC");                  
                            ?>
                <div class="col-md-4">
                    <h2>
                        <?php echo $titulo['titulo']; ?>
                    </h2>
                    <p>
                        <?php echo utf8_encode($titulo['descor']); ?>. </p>
                    <p><a class="btn btn-secondary" href="#" role="button">Ver detalles &raquo;</a></p>
                </div>
                <?php   
                    }
            ?>
        </div>
        <hr>
    </div>


With the Kevin query what the code shows is this:
Fixed as follows:

<?php
include_once('includes/conectar.php');
    $link=conectar();
?>
    <div class="container">
        <div class="row">
            <?php
                        $dc=mysqli_query($link,"Select * from tbl_noticias_blog where estatus='importante' Order By id DESC limit 3");         
                            while($titulo=mysqli_fetch_array($dc)){   
            ?>

                <div class="col-md-4">
                    <h2>
                        <?php echo $titulo['titulo']; ?>
                    </h2>
                    <p>
                        <?php echo utf8_encode($titulo['descor']); ?>. </p>
                    <p><a class="btn btn-secondary" href="#" role="button">Ver detalles &raquo;</a></p>
                </div>
                <?php   
                            }
            ?>
        </div>
        <hr>
    </div>
    
asked by felipe andrade 23.08.2017 в 14:20
source

1 answer

1

As I understand the query that could serve you is the following:

SELECT * FROM tblnoticias_blog ORDER by id DESC LIMIT 3

In which we select everything from the table and order it in descending order with a maximum of three records that in this case would be the last three.
Edito
This way you can implement it.

<div class="row">
    <?php
    $obtenertitulo=mysqli_query($link,"SELECT * FROM tblnoticias_blog ORDER by id DESC LIMIT 3");
    while ($titulo=mysqli_fetch_assoc($obtenertitulo)) {                   
        ?>
        <div class="col-md-4">
            <h2>
                <?php echo $titulo['titulo']; ?>
            </h2>
            <p>
                <?php echo utf8_encode($titulo['descor']); ?>. </p>
            <p><a class="btn btn-secondary" href="#" role="button">Ver detalles &raquo;</a></p>
        </div>
        <?php   
    } 
    ?>
</div>

wait for you to serve

    
answered by 23.08.2017 / 14:43
source