How to send more than one value by Return

Asked

Viewed 54 times

0

I am performing a query in the bank and want to returns more than one query value as I do it?

The structure of my table and the following

Id_idx | name | OUTROS |

Each Id_idx can be repeated up to 3 times I want to count how many rows are returned which is the first part of code and also returns the values of the other columns.

Here will only returns the number of rows.

$char = $pdoG->prepare("SELECT * FROM u_hero WHERE id_idx = :id");
$char->bindValue(":id",$id);
$char->execute();
$chars = $char->rowCount();
return $chars;

While you’re here you return the information.

$char = $pdoG->prepare("SELECT * FROM u_hero WHERE id_idx = :id");
$char->bindValue(":id",$id);
$char->execute();
$chars = $char->fetchAll(PDO::FETCH_OBJ);
return $chars;

How do I unite these return in a single static function?

1 answer

4


You can use an array on return:

$char = $pdoG->prepare("SELECT * FROM u_hero WHERE id_idx = :id");
$char->bindValue(":id",$id);
$char->execute();

return array(
    'datos' => $char->fetchAll(PDO::FETCH_OBJ);
    'total' => $char->rowCount()
);

When the function returns you can iterate the data, something like:

Total: <?php echo $variavel['total']; ?><br>

<!-- vai iterar os resultados -->
<?php foreach ($variavel['dados'] as $linha): ?>

    <!-- vai iterar os valores por coluna -->
    <?php foreach ($linha as $coluna => $valorColuna): ?>
    <?php echo $coluna, ': ', $valorColuna; ?><br>
    <?php endforeach; ?>

<?php endforeach; ?>

If you want to consult more than one ID you can do so:

function minhaFuncao(array $ids) {
    $query = str_repeat('id_idx = ? OR ', count($ids));
    $query = substr($query, 0, -4);//Remove o OR extra do final

    $char = $pdoG->prepare("SELECT * FROM u_hero WHERE " . $query);

    foreach ($ids as $id) {
        $char->bindValue(1, $id);
    }

    $char->execute();

    return $char->fetchAll(PDO::FETCH_OBJ);
}

And so I would consult:

$idsParaConsulta = array(
   1, 5, 8, 4, 20
);

print_r(minhaFuncao($idsParaConsulta));

  • Inside face line has the play id and it can have 3 Hero so id_idx is the play id so it comes up to 3 query with different data however id_idx equal.

  • I think now it will get better from someone help me, I thought that way would be enough apologies.

  • well 3 Heros why can be created up to 3 Heros = 3 rows from the database, I have a common id which is the id_idx want to count and take independent data from each line.

  • Guilherme just want to add $chars = $char->rowCount(); with $chars = $char->fetchAll(PDO::FETCH_OBJ).

  • The last question for me to accept your answer, is there any way I can access the data of each independent line? @Guilherme

  • i did so, foreach ($chars["data"] as $info) { echo $info->id_idx." </br>"; echo $info->name; } my doubt is like selecting the first line and the second line.

  • @Evertonfigueiredo made an example with 2 foreachs, see if this is what you need.

  • 1

    That’s exactly what I’ve been wanting so much thanks and sorry about all the hard work bro.

  • can access the array within the echo $chars['data']->id_idx array;?

  • 1

    @Evertonfigueiredo doesn’t because inside data has an index array, which goes type 0, 1, 2, ..., it can do so: $chars['dados'][0]->id_idx and the $chars['dados'][1]->id_idx catch the next.

Show 5 more comments

Browser other questions tagged

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