How to gather data from two queries in a php object?

Asked

Viewed 311 times

1

I’m doing two comic book queries at different tables. With the data of the first query I am taking data in the other, however, I can not join all the data in a json object only.

<?php
include_once("con.php");

$pdo = conectar();

$pegaContaAdmin=$pdo->prepare("SELECT * FROM contaPadrao");
$pegaContaAdmin->execute();

$return = array();

while ($linhaContaAdmin=$pegaContaAdmin->fetch(PDO::FETCH_ASSOC)) {

        $idcontaPadrao = $linhaContaAdmin['idcontaPadrao'];
        $contaPadrao = $linhaContaAdmin['contaPadrao'];
        $idsubcategoriaPadrao = $linhaContaAdmin['subcategoriaPadrao_idsubcategoriaPadrao'];

        $return[] = array(
                'idcontaPadrao' => $idcontaPadrao,
                'contaPadrao'   => $contaPadrao,
            );

    $pegaDadosSubcategoria=$pdo->prepare("SELECT * FROM subcategoriaPadrao 
                                          WHERE idsubcategoriaPadrao=:idsubcategoriaPadrao");
    $pegaDadosSubcategoria->bindValue("idsubcategoriaPadrao", $idsubcategoriaPadrao);
    $pegaDadosSubcategoria->execute();


    while ($linhaDadosSubcate=$pegaDadosSubcategoria->fetch(PDO::FETCH_ASSOC)) {

            $subcategoriaPadrao = utf8_encode($linhaDadosSubcate['subcategoriaPadrao']);
            $tipo = $linhaDadosSubcate['tipo'];

        }
}

$return['subcategoriaPadrao'] = $subcategoriaPadrao;
$return['tipo'] = $tipo;

echo json_encode($return);
?>
  • What is the result you are getting and what should it be? At first, the line you insert idcontaPadrao and contaPadrao will insert into a pre-existing index (0), but there will be no conflict as there is already an array there.

  • I updated the post and put the results that is appearing. However, should come 2 results and not only 1.

  • I don’t see "errors" in your code, maybe mistakes. For example, the query $pegaContaAdmin will only bring the last result, since it is in a while and always fill in the same variables. Already in the query $pegaDadosSubcategoria, is looking for idsubcategoriaPadrao, and apparently has only one record.

  • How do you suggest I get all the $pegaContaAdmin records? ?

1 answer

1

As the messages in the comments, the problem may be the use of while not be chained. To facilitate some things, I would also change the structure a little.

First, it would bring only the necessary data:

$pegaContaAdmin = $pdo->prepare("SELECT idcontaPadrao , contaPadrao , subcategoriaPadrao_idsubcategoriaPadrao FROM contaPadrao");

Since the idea is to return all the data, would already use the own PDO for this:

$return = $pegaContaAdmin->fetchAll(PDO::FETCH_ASSOC);
//nunca se esqueça de fechar o cursor, pode gerar problemas.
$pegaContaAdmin->closeCursor();

For subcategory data, instead of a while, I’d use a foreach, inserting the data into the existing array:

//o prepare pode ser deixado do lado de fora do foreach, apenas a variável que conterá o bind mudará.
$pegaDadosSubcategoria = $pdo->prepare("SELECT subcategoriaPadrao , tipo FROM subcategoriaPadrao WHERE idsubcategoriaPadrao=:idsubcategoriaPadrao");

//sendo $row passada como referência para poder alterar o valor do array $return
foreach($return as &$row)
{
    $pegaDadosSubcategoria->bindValue("idsubcategoriaPadrao", $row['idsubcategoriaPadrao');
    $pegaDadosSubcategoria->execute();

    while ($linhaDadosSubcate = $pegaDadosSubcategoria->fetch(PDO::FETCH_ASSOC)) {
        //realiza o encode que já existe em seu código
        $linhaDadosSubcate['subcategoriaPadrao'] =  utf8_encode($linhaDadosSubcate['subcategoriaPadrao']);

        //cria uma lista de subcategorias dentro de cada registro da categoria padrão
        $row['subcategoria'][] = $linhaDadosSubcate;
    }
}

//nunca se esqueça de fechar o cursor, pode gerar problemas.
$pegaDadosSubcategoria->closeCursor();

After that, just print

echo json_encode($return);

As we speak, I believe this solves your problems.

  • I’m gonna try Gabriel

  • Gabriel, I started your code and the following message appeared on the console: angular.js:11881GET http://localhost:8888/systems/webApps/fluxo_de_box/fluxojoin_2.0/php/pegaContaAdmin.php 500 (Internal Server Error)

  • The error posted doesn’t tell me much. Remembering that the codes are just examples. You will need to check the php or apache log to verify the error.

  • I thought your code was ready kkkk which was just copy and put kkkk

  • @Gustavosevero For the most part yes, but since only one part of your code was posted, I could only realize about what you went through.

  • But what I posted was all my php code, nothing is missing.

  • Anyway, I just pointed out minor points of amendment.

  • Gabriel, you can make the changes based on my code?

  • No, I can’t. The intention is to help and teach. Do as you can, bring the codes and/or errors. So, then, we will help you with the mistakes. What’s more, the example code is already 100% of your code, only parts of it.

  • 1

    Yeah, but unfortunately, it didn’t help.

Show 5 more comments

Browser other questions tagged

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