Select certain MySQL table rows

0

Well, I have the following code:

function dameTodosLosProyectos( $con, $cantidad = 0 )
{
    if( $cantidad > 0 )
    {
    $query = "SELECT * FROM proyectos LIMIT $cantidad";
    }else
        {
        $query = "SELECT * FROM proyectos";
        }
    return mysqli_query($con, $query);
 }

With him I can put so much:

   $result = dameTodosLosProyectos($link, 3); //Seleccionar primeras 3 filas
   $result = dameTodosLosProyectos($link); //Seleccionar todas las filas

Now, my question is:

  • How do I select three rows but not the first three but the last three or starting with the second (that is, the 2,3 and 4)?
  • How do I select for example rows 2 and 3, to print them with a certain CSS, and then take the 4 and apply another code, etc.?
asked by Adrian 06.02.2018 в 12:49
source

1 answer

1

In the definition of the function, add an offset:

function dameTodosLosProyectos( $con, $cantidad = 0, $offset = 0 )

Then adapt the SQL:

$query = "SELECT * FROM proyectos LIMIT $cantidad OFFSET $offset";

So when you do:

$result = dameTodosLosProyectos($link, 3);

You can also tell the offset:

$result = dameTodosLosProyectos($link, 3, 15);

With what it will return from 16, 3 records.

You can also define the SQL like this:

$sql = "SELECT * FROM Orders LIMIT $offset, $canitidad";
    
answered by 06.02.2018 в 13:32