Reputation: 375
I have two queries that I want to run. The result should be that the rows of dilemmas/questions are listed with the answers below the questions. The answers are hidden though by javascript until I choose to press the image-button, which should then show the answer that belongs to the chosen question.
The code:
$result = mysqli_query($mysqli,"SELECT rid, pid, qid, aid, points FROM result WHERE rid=$val");
$answertext = mysqli_query($mysqli, "SELECT answer FROM answer_det WHERE pid=(SELECT pid FROM result WHERE rid=$val) AND qid=(SELECT qid FROM result WHERE rid=$val) AND aid=(SELECT aid FROM result WHERE rid=$val)");
while($row = mysqli_fetch_array($result) AND $row2 = mysqli_fetch_array($answertext))
{
$resultp = $row['points'];
$color = "#000000";
if (($resultp >= 1) && ($resultp <= 3))
$color = "#FF0000";
else if (($resultp >= 3) && ($resultp <= 6))
$color = "#FF9900";
else if (($resultp >= 6) && ($resultp <= 10))
$color = "#07d407";
echo "<div class='question-wrap'>
<b><small>Dilemma ".$row['qid']." - Answer ". $row['aid'].": </small><span style=\"color: $color\">". $resultp."</span></b> of <b>10 <small>Points</small></b>
<input type='image' class='answer-toggle' title='Information' src='img/down.png' width='13' height='10'>
<p class='answer'>". $row2['answertext']."</p></div>"; }
I can't figure out what is wrong. This is the message I get:
Warning: mysqli_query(): (21000/1242): Subquery returns more than 1 row in D:\home\site\wwwroot\devlopment\respondent2.php on line 122 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\home\site\wwwroot\devlopment\respondent2.php on line 125
This is line 122:
$answertext = mysqli_query($mysqli, "SELECT answer FROM answer_det WHERE pid=(SELECT pid FROM result WHERE rid=$val) AND qid=(SELECT qid FROM result WHERE rid=$val) AND aid=(SELECT aid FROM result WHERE rid=$val)");
This is line 125:
while($row = mysqli_fetch_array($result) AND $row2 = mysqli_fetch_array($answertext))
Upvotes: 1
Views: 3128
Reputation: 1311
Remove the subqueries and do a left join on the table result or do this (you will only take the first line that appears):
$answertext = mysqli_query($mysqli, "SELECT answer FROM answer_det WHERE pid=(SELECT top 1 pid FROM result WHERE rid=$val) AND qid=(SELECT top 1 qid FROM result WHERE rid=$val) AND aid=(SELECT top 1 aid FROM result WHERE rid=$val)");
Your subqueries can't return more than one value to compare or anything else.
Upvotes: 0
Reputation: 18584
When you use a query like
WHERE your_column = (SELECT ... WHERE ...)
The subselect must return only one row; if it does not, then you get the error you are seeing.
A quick solution could be to change it to
WHERE your_column = (SELECT ... WHERE ... LIMIT 1)
but I'd use joins instead:
SELECT answer
FROM answer_det
JOIN result USING (pid, qid, aid)
WHERE result.rid = $val
Upvotes: 2