duangeli1334 2016-03-08 17:25
浏览 80
已采纳

子查询返回多于1行的错误

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\devlopmentespondent2.php on line 122 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\home\site\wwwroot\devlopmentespondent2.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))
  • 写回答

2条回答 默认 最新

  • draj30958 2016-03-08 17:36
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?