查询在正确执行时不会获取数据
I find it very strange but something does not feel right. I want to populate a JSON Array with data from mysql. The first query will bring data for categories and questions and then for each question i want to get the answers. I get the data from 1st query but from second i do not.
My code:
<?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']
));*/
?>
UPDATE
I managed to fix it with this code:
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']);
}
}
Now i want for each item in the a array the array "question_answers" to be populated with the values of each item of the array times.
I have tried this:
foreach ($times as $w => $e) {
$a['question_answers'][] = array("answer" => $e['answer'], "iscorrect" => $e['iscorrect']);
}
But it does not give me the desired result.
I want result to be like this:
"category_name" => categoryname,
"category_id" => categoryid,
"question_name" => questionname,
"question_answers" =>[
"answer" => answer1,
"iscorrect" => yes,
"answer" => answer2,
"iscorrect" => no,
"answer" => answer3,
"iscorrect" => no,
]
How is this possible to do. With the last method i tried it does not work. gives me empty array.
I have tried while loops and foreach but still nothing. I would appreaciate any help!
Finally i managed to solve it like this:
I executed the 1st query that returned 2 arrays for me. 1 with the normal data and 1 with just questions in it using this code:
$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'];
}
Then i looped through the second array with another foreach to execute second query like this:
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;
}
}
and then i got the desired result. Thanks everyone for your time.
Hope i help someone!!!
I believe that the problem is that you're using the same PDO resource for both queries. Try adding $handler->closeCursor()
before your foreach
loop.
You should also probably be using a prepared statement within the loop.