this is my database design for a ques n ans page.
A user can ask as many questions, receive as many answers to a single question.
ie. one question and many answers.
All i want to know is mysql query that’ll be required for fetching the answers for a particular database. Also the php code that’ll be involved in sending and thereby retrieving the results.
db design:
create table user (
name varchar(14) not null.
id int(11) auto_increment;
CREATE TABLE questions (
question_id int(11) NOT NULL AUTO_INCREMENT,
user_id int(11) references user(id),
title varchar(255),
content text,
PRIMARY KEY (question_id);
CREATE TABLE answers (
answer_id int(11) NOT NULL AUTO_INCREMENT,
question_id int(11) REFERENCES questions(question_id),
content text,
PRIMARY KEY (answer_id);
If you already know the ID for the particular question, then all you need to do is select all the records in
answerswith that question_id. If you’re selecting based on some other attribute of the question, then you’d select that question fromquestionswithLEFT JOIN answers on questions.question_id = answers.question_idin order to get all the relevant answers.As for PHP code, read up on mysqli or PDO. This should be a textbook case of running a query and then fetching the results. Check out example 1 on this page for how to create a DB connection and example 1 on this page for a very simple query example. But note that if you’re searching on a user-provided string, a simple query like that creates huge security holes! You should use the prepare() function instead.