0
I have the following querys:
<?php
$VarMensagem = 1;
$pdo = new PDO($dsn, $un, $pwd, $opt);
$data = array();
$dataGeral = array();
try {
$stmt = $pdo->query("SELECT * FROM mensagem WHERE mensagem_id ='{$VarMensagem}'");
while($row = $stmt->fetch(PDO::FETCH_OBJ))
{
$data[] = $row;
$VarMinhasResp = $data[]=$row->respostas;
$QueryRespostas = $pdo->query("SELECT id AS ID,descricao AS DESCRICAO FROM respostas WHERE id IN ('{$VarMinhasResp}')");
while($row = $QueryRespostas->fetchall(PDO::FETCH_OBJ))
{
$dataGeral['respostas'] = $row;
}
}
$result = array_merge($data , $dataGeral);
echo json_encode($result);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
What I do, I go in the messages table and I get the id of the answers and I make a query in the answers, this working, but this returning only the first record, follows how it is the right of the query and that runs perfectly in mysql:
SELECT id AS ID,descricao AS DESCRICAO FROM respostas WHERE id IN (1,5,10,11,15)
I cannot see where the error is, whether it is in the loop or in the array, which can be ?
Why are you running the query within the
while
? It shouldn’t be outside?– Woss
There are several mistakes, they are not separated by
,
and the execution is within thewhile
. In addition you can nor need PHP to do this.– Inkeliz
@ Anderson Carlos Woss I tested out and also was not
– Chefe Druida
@ Inkeliz I did as you said only with sql and still only returned the first record
– Chefe Druida
$dataGeral['respostas'] = $row;
you are assigning the value in the same variable every time of the loop.– edson alves