Jump to content

Recommended Posts

I'm doing the following query where "ctext" comes from the clues table and "answerid" and "atext" come from the answers table...

 

	$sql = "SELECT * from clues, answers 
					WHERE clues.quizid = '{$_GET['quizid']}'
					AND answers.quizid = '{$_GET['quizid']}'";
					$result = mysql_query($sql, $connection);
					if (!$result) { 
					die("Database query failed: " . mysql_error());
					} else {

						while ($info=mysql_fetch_array($result)) {

						echo "<tr><td>" . $info['answerid'] . "</td>";

						echo "<td>".$info['ctext']."</td>";

						echo "<td>".$info['atext']."</td>";

						}
						echo "</tr>";
						}
					}

 

But when this displays in the browser, it's outputting each result set twice and kinda mixed up.  For example, it looks like this...

 

1 monkey funny

1 cat funny

2 monkey boring

2 cat boring

 

But I want (i.e. was expecting) it to display as...

 

1 monkey funny

2 cat boring

 

Can anyone tell me why it's showing TWO rows for each and seemingly mixing  up the returned results?

 

Ultimate question is this.  Do I have to break apart the mysql queries in order to get PHP to display the results how I want?  I gotta think there is a way to do just one query and do what I want, but I obviously can't figure it out :)

 

 

If I put the code right into mysql, it brings back the same thing, i.e. 4 rows instead of what I want (i.e. 2 rows).

 

Clearly something with my syntax is wrong, but not sure what change I would make to only get 2 rows.

 

I could split it out into 2 separate queries, but that seems so inefficient (but maybe it's not :) )

 

I'll mess with it some more but if anyone can see my stupid mistake, let me know  :P

table "clues" structure:

clueid, quizid,cluetext

 

actual data example:

Row 1: 1,16,funny

Row 2: 2,16,boring

 

table "answers" structure:

answerid, quizid, answertext

 

actual data example:

Row1: 1,16,monkey

Row 2: 2, 16,cat

 

So based on that, I just want to get this back from my query...

 

1 funny monkey

2 boring cat

 

Instead, I'm getting back...

 

1 funny monkey

2 boring monkey

1 funny cat

2 boring cat

Try this:  Matches the clueid to the answerid, which would match funny to monkey, and boring to cat.

SELECT * from clues, answers
                  WHERE clues.clueid = answers.answerid
                  AND clues.quizid = '{$_GET['quizid']}'

Got it!  You loosened it for me ;)  Your last suggestion brought back other answers that had an answerid of 1 or 2, even if they didn't have a quizid of 16.  So I added one more line to the query and this worked...

 

SELECT * from clues, answers
					  WHERE clues.clueid = answers.answerid
					  AND clues.quizid = '{$_GET['quizid']}'
					  AND answers.quizid = '{$_GET['quizid']}'

 

Thanks so much for your help!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.