Code only returns me the first row of the table referring to the patient ID

Asked

Viewed 127 times

0

The code only returns me the first row of the table referring to the patient ID

<?php
require 'conexao.php';

// Recebe o id do exame do exame via GET
$id_exames = (isset($_GET['id'])) ? $_GET['id'] : '';


// Valida se existe um id e se ele é numérico
  if (!empty($id_exames) && is_numeric($id_exames)):

    // Captura os dados do exame solicitado
    $conexao = conexao::getInstance();
    $sql = 'SELECT  id,codigo,data,exame,data2  FROM historico where id='.$id_exames;   
    $stm = $conexao->prepare($sql);
    $stm->bindValue(':id', $id_exames);
    $stm->execute();
    $exames = $stm->fetch(PDO::FETCH_OBJ);



  endif;

?>
  • 1

    I didn’t understand. Put the sql of the code and if possible speak like this your table in the database. To make it easier to help you...

  • The function of PDO fetch list only one element, to list all that return in the query, you must use: fetchAll

2 answers

4

You have two options to take more than one datum in this context.

One of them is to do this:

while( $exames = $stm->fetch(PDO::FETCH_OBJ) ) {
    // este loop vai ser executado uma vez para cada linha.
    print_r( $exames ); // para você ver como o resultado vem neste caso
}

Handbook:

http://php.net/manual/en/pdostatement.fetch.php


The other is to recover everything in one structure:

$exames = $stm->fetchAll()
print_r( $exames ); // para você ver como o resultado vem neste caso

Handbook:

http://php.net/manual/en/pdostatement.fetchall.php

  • Hello, thank you so much

  • All records referring to the patients were shown, but did not appear inside the table as before <td><?= $exams->code? ></td> <td><?= $exams->date? ></td> <td><?= $exams-> <td><?= $exams->data2? ></td>

0

<?php
require 'conexao.php';

// Recebe o id do exame do exame via GET
$id_exames = (isset($_GET['id'])) ? $_GET['id'] : '';


// Valida se existe um id e se ele é numérico
  if (!empty($id_exames) && is_numeric($id_exames)):

    // Captura os dados do exame solicitado
    $conexao = conexao::getInstance();
    $sql = "SELECT  id,codigo,data,exame,data2  FROM historico where id=:id";   
    $stm = $conexao->prepare($sql);
    $stm->bindValue(':id', $id_exames, PDO::PARAM_INT);
    $stm->execute();
    $exames = $stm->fetchAll(PDO::FETCH_OBJ);



  endif;

?>

Browser other questions tagged

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