Sql query along with $_GET['id'] or something else?

Hi everyone

I know how to pass $_GET[‘id’] variable to sql query. It is like this:

	SELECT historical_epoch, picture_link, picture_title, picture_author
		FROM historical_epochs
		WHERE id = {$_GET['id']};

and this:


	SELECT questions.id, questions.question
		FROM epochs JOIN  questions
		ON epochs.id = questions.epoch_id
		AND epochs.id = {$_GET['id']}
		ORDER BY RAND()
		LIMIT 2;

But how to pass something else eg. ‘answers.question_id = 3’ to sql query? What should I do with ‘answers.question_id = 3’?


	SELECT answer, boolean_type FROM questions JOIN answers
		ON questions.id=answers.question_id
		WHERE [B]answers.question_id = 3[/B] ORDER BY RAND();

Please help.

Thanks in advance for yours reply.

The same way. $_GET is a global aray containing the query string name=>value pairs. Use the key for the appropriate $_GET array key that you would like to embed. Also, look into mysql_real_escape_string() to prevent injection. Its never wise to just embed user controlled data into a SQL statement.

I want this:

$displayEpoch = new DisplayEpoch($host, $username, $password, $database);

$id = $_GET['id'];

$displayEpoch->sql = "SELECT historical_epoch, picture_link, picture_title, picture_author
						FROM historical_epochs
						WHERE id = {$id};";

$question_id = rand(1, 11);  // because I have 11 questions

$displayEpoch->sql .= "SELECT questions.id, questions.question
				FROM epochs JOIN  questions
				ON epochs.id = questions.epoch_id
				AND epochs.id = {$id}
				AND questions.id = {$question_id}
				LIMIT 1;";

$displayEpoch->sql .= "SELECT answer, boolean_type
				FROM questions JOIN answers
				ON questions.id=answers.question_id
				WHERE answers.question_id = {$question_id} limit 4;";

$displayEpoch->Display();

I resolved one problem and come across on another. Such is life
Thanks

what is the other problem?

also, please note, when posting a question in the mysql forum, showing us raw php isn’t the best strategy

instead, you should echo the $sql variable, and let us see the actual mysql SQL

The other problem is that:

The tables given below shows that every rows in the ‘questions’ table is assigned to four rows in the ‘answers’ table. I wonder how to select randomly four questions and sixteen answers and then display them separately. I want to gain the following effect:

first site:
1 question - 1 answer
- 1 answer
- 1 answer
- 1 answer

second site:
3 question - 3 answer
- 3 answer
- 3 answer
- 3 answer

third site:
2 question - 2 answer
- 2 answer
- 2 answer
- 2 answer

fourth site:
4 question - 4 answer
- 4 answer
- 4 answer
- 4 answer

If I write this query:

SELECT question FROM questions WHERE epoch_id = 2 AND id = 4;

then the result is:
question number 4

If I write this query:

SELECT answer FROM questions JOIN answers ON questions.epoch_id = 2 AND questions.id=answers.question_id AND answers.question_id = 4;

then the result is four times:
answer number 4
answer number 4
answer number 4
answer number 4

What do I have to do if I want to gain more rows from the ‘answers’ table?

CREATE TABLE epochs (id INT AUTO_INCREMENT NOT NULL,
epoch VARCHAR(100),
date_ TIMESTAMP NOT NULL,
refer_to_picture TEXT,
title_picture TEXT,
author_picture TEXT,
PRIMARY KEY (id)) ENGINE=InnoDB;

CREATE TABLE questions (id INT AUTO_INCREMENT NOT NULL,
epoch_id INT,
question TEXT,
date_ TIMESTAMP NOT NULL,
PRIMARY KEY (id),
INDEX (epoch_id),
FOREIGN KEY (epoch_id)
REFERENCES epochs(id)
ON DELETE RESTRICT
ON UPDATE CASCADE) ENGINE=InnoDB;

CREATE TABLE answers (id INT AUTO_INCREMENT NOT NULL,
question_id INT,
answer TEXT,
boolean_type BOOL,
date_ TIMESTAMP NOT NULL,
PRIMARY KEY (id),
INDEX (question_id),
FOREIGN KEY (question_id)
REFERENCES questions(id)
ON DELETE RESTRICT
ON UPDATE CASCADE) ENGINE=InnoDB;

I understood that

this is not the place to ask questions about a problem you’re having

Sorry and thanks for attentions.