PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE questions ( id int primary key auto_increment, parent_test int ); INSERT INTO questions (parent_test) VALUES (1),(1),(1); CREATE TABLE answers ( id int primary key auto_increment, parent_questions int, correct_answer int ); INSERT INTO answers (parent_questions, correct_answer) VALUES (1, 1),(1,2),(1,3);
Copy Clear
Copy Format Clear
<?php function get_answers($db, $test) { if (!$test) { return false; } $query = "SELECT q.id AS questions_id, a.id AS answers_id FROM questions q LEFT JOIN answers a ON q.id = a.parent_questions WHERE q.parent_test = $test AND a.correct_answer IN ('1', '2', '3', '4', '5') "; $res = mysqli_query($db, $query); $data = null; while ($row = mysqli_fetch_assoc($res)) { $data[$row["questions_id"]] = $row["answers_id"]; } return $data; } $res = get_answers($mysqli, 1); print_r($res);
Show:  
Copy Clear