我觉得很奇怪,但感觉不对劲。我想用来自mysql的数据填充JSON数组。第一个查询将为类别和问题提供数据,然后针对每个问题我想获得答案。我从第一个查询中获取数据,但从第二个查询中获取数据。
我的代码:
<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set("default_charset", "UTF-8");
header('Content-type: text/html; charset=UTF-8');
try {
$handler = new PDO('mysql:host=localhost;dbname=database', 'root', '');
$handler->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8");
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$handler->exec("SET CHARACTER SET 'utf8'");
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$query = $handler->query('SELECT DISTINCT c.cat_name, c.cat_id, q.question FROM `categories` c
LEFT JOIN `questions` q ON c.cat_id = q.cat_id WHERE c.cat_id = 1');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($records);
echo "</pre>";
$answers = array();
foreach($records as $k => $v){
$ques = $v['question'];
$ques = trim($ques);
$qu = $handler->query("SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."' ");
echo "SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."'<br>";
$answers = $qu->fetch(PDO::FETCH_BOTH);
/*$answers = $qu->fetchAll(PDO::FETCH_ASSOC);
foreach ($answers as $key => $value) {
echo "Key: " . $key . " Value: " . $value;
}
//$answersR = $qu->fetchAll(PDO::FETCH_ASSOC);*/
echo "<pre>";
print_r($answers);
echo "</pre>";
}
$j['quiz'] = $json;
echo json_encode($j);
/*$json[] = array(
"category_name" => $v['cat_name'], "category_id" => $v['cat_id'], "question_name" => $v['question'],
"answers" => array(
"answer" => $answers['answer'],
"iscorrect" => $answers['iscorrect']
));*/
?>
更新
我设法使用此代码修复它:
foreach($records as $k => $v){
$a[] = array("category_name" => $v['cat_name'], "category_id" => $v['cat_id'], "question_name" => $v['question'], "question_answers" => array() );
$normal[] = $v['question'];
}
foreach ($normal as $key => $value) {
$ques = $value;
$qu = $handler->query("SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."' ");
$ans = $qu->fetchAll(PDO::FETCH_ASSOC);
foreach ($ans as $key => $value) {
$times[] = array('answer' => $value['answer'], 'iscorrect' => $value['iscorrect']);
}
}
现在我想要在数组中的每个项目中使用数组的每个项目的值来填充数组“question_answers”。
我试过这个:
foreach ($times as $w => $e) {
$a['question_answers'][] = array("answer" => $e['answer'], "iscorrect" => $e['iscorrect']);
}
但它没有给我预期的结果。
我希望结果如下:
"category_name" => categoryname,
"category_id" => categoryid,
"question_name" => questionname,
"question_answers" =>[
"answer" => answer1,
"iscorrect" => yes,
"answer" => answer2,
"iscorrect" => no,
"answer" => answer3,
"iscorrect" => no,
]
这怎么可能呢。用我试过的最后一个方法它不起作用。给我空数组。
我试过while循环和foreach但仍然没有。我会提供任何帮助!
答案 0 :(得分:4)
我认为问题在于您为两个查询使用相同的PDO资源。尝试在$handler->closeCursor()
循环之前添加foreach
。
你也应该在循环中使用预备语句。
答案 1 :(得分:0)
最后我设法解决了这个问题:
我执行了第一个为我返回2个数组的查询。 1使用此代码,其中包含正常数据,1只包含问题:
$query = $handler->query('SELECT DISTINCT c.cat_name, c.cat_id, q.question FROM `categories` c
INNER JOIN `questions` q ON c.cat_id = q.cat_id WHERE c.cat_id = 1');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
$a = array();
$ans = array();
foreach($records as $k => $v){
$first[] = array("category_name" => $v['cat_name'], "category_id" => $v['cat_id'], "question_name" => $v['question'], "question_answers" => array());
$second[] = $v['question'];
}
然后我用另一个foreach循环遍历第二个数组,执行第二个查询,如下所示:
foreach ($second $key => $value) {
$ques = $value;
$qu = $handler->query("SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."' ");
$third = $qu->fetchAll(PDO::FETCH_ASSOC);
foreach ($first as $k => $v) {
$first[$key]['question_answers'] = $third;
}
}
然后我得到了理想的结果。谢谢大家的时间。
希望我帮助别人!!!