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') "; // Create a statement $stmt = $db->prepare($query); // Bind params and execute $stmt->bind_param("i", $test); // Extract result set and loop rows $result = $stmt->get_result(); $data = mysqli_fetch_all($result); return $data; } $res = get_answers($mysqli, 1); print_r($res);
Show:  
Copy Clear