Data return en JSON in an array , uploading the database ID by the respective Names. How can I do this?

Asked

Viewed 38 times

1

I made this code, but it only returns me the ids, and I would like in the place of the ids he did a search by Ner Join in the database and returns in place of the ids the names.

Php code:

<?php
header('Content-Type: application/json');
require 'settings/Conexaobd.php';
global $connect;

$result = $connect->query("SELECT * FROM agendamento");
$linhas = $connect->query("SELECT COUNT(*) FROM agendamento");

$row = $linhas->num_rows;

if($row > 0){
    $json = array();

    while($ros = $result->fetch_assoc()){
        $linha = array();   
        $linha['id'] = $ros['id'];
        $linha['paciente'] = $ros["paciente"];
        $linha['medico'] = $ros["medico"];
        $linha['hospital'] = $ros["hospital"];
        $linha['especialidade'] = $ros["especialidade"];
        $linha['dt_consulta'] = $ros["dt_consulta"];
        $linha['dt_criacao'] = $ros["dt_criacao"];

        $json[] = $linha;
    }

    echo json_encode($json);
}else{
    $json['error'] = "Inativo no momento";
    echo json_encode($json);
}

mysqli_close($connect);
?>

Follow code from the database search I want to perform:

SELECT 
    paciente.nome AS Paciente,
    medico.nome AS Medico,
    hospital.descricao AS Hospital,
    especialidade.descricao AS Especialidade 
FROM 
    agendamento 
INNER JOIN 
    paciente ON agendamento.paciente = paciente.id 
INNER JOIN 
    medico ON agendamento.medico = medico.id 
INNER JOIN 
    hospital ON agendamento.hospital = hospital.id 
INNER JOIN 
    especialidade ON agendamento.especialidade = especialidade.cod_especialidade

1 answer

0

I think you forgot to put the query you want in the right place, replacing:

$result = $connect->query("SELECT * FROM agendamento");

for

$result = $connect->query("SELECT 
agendamento.id,
agendamento.dt_consulta,
agendamento.dt_criacao,
paciente.nome AS Paciente,
medico.nome AS Medico,
hospital.descricao AS Hospital,
especialidade.descricao AS Especialidade
FROM
  agendamento
INNER JOIN
  paciente ON agendamento.paciente = paciente.id
INNER JOIN
  medico ON agendamento.medico = medico.id
INNER JOIN
  hospital ON agendamento.hospital = hospital.id
INNER JOIN
  especialidade ON agendamento.especialidade = especialidade.cod_especialidade");

Browser other questions tagged

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