Select and display data from a position in the database table

Asked

Viewed 74 times

1

I need to create two tables on the same page where in the first one will have only one client, in the case, the first of the list and in the second the other customers, the problem is that when I list the second table, the first customer also appears, and I need the second table list-if from the second customer, because when the customer of the first customer is met the page reloads and the first from the bottom rises, so successively.

What I tried to:

<?php
session_start();
require 'conexao.php';
$conexao = conexao::getInstance();
    $sql = 'SELECT id_agenda, hr_agendamento ,nome_cliente ,realizado FROM agenda
    WHERE nome_cliente = "nome_cliente "   and realizado ="N"  
     limit 1
    ';
    $stm = $conexao->prepare($sql);
    $stm->execute();
    $clientes = $stm->fetchAll(PDO::FETCH_OBJ);
$conexao2 = conexao::getInstance();
    $sql = 'SELECT id_agenda, hr_agendamento ,nome_cliente ,realizado FROM agenda
    WHERE nome_cliente = "nome_cliente "   and realizado ="N"  
     limit 3
    ';
    $stm = $conexao2->prepare($sql);
    $stm->execute();
    $clientes2 = $stm->fetchAll(PDO::FETCH_OBJ);

?>

How are the tables getting:

TABELA1
ID HORARIO CLIENTE REALIZADO
1  07:00    MARIA   NAO
TABELA2
ID HORARIO CLIENTE REALIZADO
1  07:00    MARIA   NAO
2  07:00    JOAO    NAO
3  07:00    CARLOS  NAO

1 answer

1


You can use the LIMIT to do this kind of thing too, see how the query would look:

$sql = 'SELECT id_agenda, hr_agendamento, nome_cliente, realizado FROM agenda WHERE nome_cliente="nome_cliente" AND realizado="N" LIMIT 1, 3';
  • What does it mean LIMIT 1, 3?

The query will select from element 1 (starts from 0) and display at most 3 elements.

And the OFFSET also works the same way:

$sql = 'SELECT id_agenda, hr_agendamento, nome_cliente, realizado FROM agenda WHERE nome_cliente="nome_cliente" AND realizado="N" LIMIT 3 OFFSET 1';

Obs: I took the query of your question to base me, but possibly it is wrong, aiming that you are comparing a column with the string "client name" and not with variables.

See how the comparison with a variable would look:

$sql = "SELECT id_agenda, hr_agendamento, nome_cliente, realizado FROM agenda WHERE nome_cliente='$nome_cliente' AND realizado='N' LIMIT 1, 3";

And if the variable is coming in the post method:

$sql = "SELECT id_agenda, hr_agendamento, nome_cliente, realizado FROM agenda WHERE nome_cliente='{$_POST['nome_cliente']}' AND realizado='N' LIMIT 1, 3";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.