Tiago Silveira
Tiago Silveira

Reputation: 267

How to get the result of the second Query

I'm trying to execute two select statements but it shows me only the first one. I tryed to use foo->nextRowset(); but it doesn't work, he stops show me the first query. how can I get the content of the 2 querys? I'm doing this way cuz I need to merge this content on my front end.

PHP:

<?php
header('Content-Type: application/json');

$dificuldade = $_GET ['dificuldade'];//Define a dificuldade das perguntas que seram selecionadas

$numquestions = $_GET ['rodada'];//Número de perguntas que serão retornadas


switch ($numquestions) {
    case '1':
    $numquestions = "10";
    break;
    case '2':
    $numquestions = "15";
    break;
    case '3':
    $numquestions = "20";
    break;
}
try{
    $conexao = new PDO ("mysql:host=localhost; dbname=teocratico; charset=utf8","root","");
} catch (PDOException $erro){
    echo $erro->getmessage();
}

$stmt = $conexao->query ("SELECT
    perguntas.id_pergunta,
    perguntas.pergunta,
    perguntas.resposta,
    dificuldade.dificuldade
    FROM perguntas
    JOIN dificuldade
    ON dificuldade.id_dificuldade = perguntas.dificuldade
    WHERE dificuldade.id_dificuldade = $dificuldade limit $numquestions;


    SELECT descricao from desafios ORDER BY RAND();

    ");

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

$json = json_encode($data, JSON_PRETTY_PRINT);

echo $json;


?>

Upvotes: 1

Views: 51

Answers (1)

Paul Schreiber
Paul Schreiber

Reputation: 12599

You need to execute each statement separately:

$stmt = $conexao->query ("SELECT
    perguntas.id_pergunta,
    perguntas.pergunta,
    perguntas.resposta,
    dificuldade.dificuldade
    FROM perguntas
    JOIN dificuldade
    ON dificuldade.id_dificuldade = perguntas.dificuldade
    WHERE dificuldade.id_dificuldade = $dificuldade limit $numquestions;
    ");

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;

$stmt = $conexao->query ("SELECT descricao from desafios ORDER BY RAND()");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;

Upvotes: 1

Related Questions