gpt4 book ai didi

php - 如何获取第二个Query的结果

转载 作者:行者123 更新时间:2023-11-28 23:16:37 24 4
gpt4 key购买 nike

我正在尝试执行两个选择语句,但它只显示第一个。我尝试使用 foo->nextRowset(); 但它不起作用,他停止向我显示第一个查询。我怎样才能得到这两个查询的内容?我这样做是因为我需要在我的前端合并这些内容。

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;


?>

最佳答案

您需要分别执行每个语句:

$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;

关于php - 如何获取第二个Query的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com