查询不按顺序返回

时间:2013-06-18 09:08:38

标签: php mysql

如果您需要任何其他信息来帮助我,请告诉我。但是,该项目需要受密码保护。

此刻我感到非常困惑,因为我在一个未完成项目的这一部分中有一段功能正常的代码,或者我认为。我正在寻找有助于解决这个问题的任何建议。

我没有得到“理智”的结果,我做了两次相同的事情,得到了不同的结果。这两段代码都成功地返回了SAME问题的所有答案,但其中一个代码在确切的代码块中按顺序显示17,18,19,16,而第二个代码显示16,17,18,19 in正确的顺序。

在第二段代码中,它是给定问题的更详细视图的一部分以及与之相关的所有答案(只是第一个代码的更详细视图),编辑函数将编辑正确的代码,但删除复选框失败并删除错误的项目。此模式也与故障代码的视觉排序不一致,并且看起来非常随机。这可能是相同或不同的问题,我正在研究这个问题。

如下所示,有两段代码,其中一节显示问题所有答案的数据行:

    $query2 = "SELECT * FROM ST_Answers
    WHERE referencingQuestionID = '$permIDNum'
    ORDER BY 'permanentAnsNumber'";
    $result2 = mysql_query($query2);
    $ret = "";
    $letter = "a";

    $ret .= "<div class='lq answerListItem'>";
    $ret .= "<span class='lqh2 answerNumber'> &nbsp; </span>";
    $ret .= "<span class='lqh2 answerTextBody'>Answer Text Body</span>";
    $ret .= "<span class='lqh2 answerSummaryText'>Answer Summary Text</span>";
    $ret .= "<span class='lqh2 answerNextQuestion'>Next Q#</span>";
    $ret .= "<span class='lqh2 correct'>Correct?</span>";
    $ret .= "</div>";


    $i = 0;
    $n = mysql_numrows($result2);
    while( $i < $n ){
            $permAID = mysql_result($result2,$i,"permanentAnsNumber");
        $aText = mysql_result($result2,$i,"answerTextBody");
        $aSummary = mysql_result($result2,$i,"answerSummaryText");
        $nextQID = mysql_result($result2,$i,"nextQuestionID");
        $correctA = mysql_result($result2,$i,"correctAnswer");

        $ret .= "<div class='lq answerListItem'>";
        if($letter != "a") { $ret .= "<br/>"; }
        $ret .= "<span class='lq answerNumber'> &nbsp; </span>";
            $ret .= "<span class='lq answerTextBody'>" . $letter . ") $aText </span>";
        $ret .= "<span class='lq answerSummaryText'>" . $aSummary . "</span>";
        $ret .= "<span class='lq answerNextQuestion'>" . $nextQID . "</span>";
        $ret .= "<span class='lq correct'>" . $correctA . "</span>";
        $letter++;
        $ret .= "</div>";
        $i++;
    }

这第二段代码,使用SAME查询显示相同的数据但更多,但不是按顺序显示项目,而是以正确的顺序正确显示它们,但代码几乎完全相同!

$query2 = "SELECT * FROM ST_Answers
    WHERE referencingQuestionID='$permID' 
    ORDER BY 'permanentAnsNumber'";
$result2 = mysql_query($query2);
$ret .= "<div id='answerContainer'><h3>Associated Answers: </h3>";

$i = 0;
$n = mysql_numrows($result2);
// NOTE: This is displaying everything in correct order
// TODO: Reference this
while( $i < $n ){
        $permAID = mysql_result($result2,$i,"permanentAnsNumber");
    $aText = mysql_result($result2,$i,"answerTextBody");
    $aSummary = mysql_result($result2,$i,"answerSummaryText");
    $nextQID = mysql_result($result2,$i,"nextQuestionID");
    $correctA = mysql_result($result2,$i,"correctAnswer");

    $ret .= "<div class='answerRow'>";
        $ret .= "<span class='perAID'>" . "<h5>Answer ID(Warning do not change):<textarea rows='1' cols='4' name='ids[]'>" . $permAID . "</textarea></span></h5>";
        $ret .= "<span class='aText'>" . "Answer Text: <br /><textarea name='aTxt[]' rows='5' cols='40'>" . $aText . "</textarea></span><br />";
        $ret .= "<span class='aSummary'>" . "Answer Summary: <br /><textarea name='aSum[]' rows='5' cols='40'>" . $aSummary . "</textarea></span><br />";
        $ret .= "<span class='nextQID'>Question this leads to: <textarea name='nxtQID[]' rows='1' cols='4'>" . $nextQID . "</textarea></span><br />";
        $ret .= "<span class='correctA'>" . "Answer point value: <textarea name='ansCorrect[]' rows='1' cols='2'>" . $correctA . "</textarea></span><br />";
        $ret .= "<span class='del'><h6>Delete? <input type='checkbox' name='delete[]'></h6></span><br />";
    $ret .= "</div><br />";

    $i++;
}

有没有人对从哪里开始这个有任何想法?我完全被难倒了。

4 个答案:

答案 0 :(得分:3)

您的问题是您在列名称周围使用引号。以这种方式向ORDER BY引入常量文字字符串,可以有效地消除结果集的排序。

更改

$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID = '$permIDNum' 
           ORDER BY 'permanentAnsNumber'";
                    ^                  ^

$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID = '$permIDNum' 
           ORDER BY permanentAnsNumber";

这是 SQLFiddle 演示。

注意事项:与prepared statementsPDO一起学习并使用MySQLi。不推荐使用Mysql_ *扩展名。

答案 1 :(得分:0)

我建议你改变你的风格,尝试使用这个

$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID='$permID' ORDER BY permanentAnsNumber";
$result2 = mysql_query($query2);
$ret .= "<div id='answerContainer'><h3>Associated Answers: </h3>";

while($row = mysql_fetch_assoc($result2)) {

    $ret .= "<div class='answerRow'>";
        $ret .= "<span class='perAID'>" . "<h5>Answer ID(Warning do not change):<textarea rows='1' cols='4' name='ids[]'>" . $row["permanentAnsNumber"] . "</textarea></span></h5>";
        $ret .= "<span class='aText'>" . "Answer Text: <br /><textarea name='aTxt[]' rows='5' cols='40'>" . $row["answerTextBody"] . "</textarea></span><br />";
        $ret .= "<span class='aSummary'>" . "Answer Summary: <br /><textarea name='aSum[]' rows='5' cols='40'>" . $row["answerSummaryText"] . "</textarea></span><br />";
        $ret .= "<span class='nextQID'>Question this leads to: <textarea name='nxtQID[]' rows='1' cols='4'>" . $row["nextQuestionID"] . "</textarea></span><br />";
        $ret .= "<span class='correctA'>" . "Answer point value: <textarea name='ansCorrect[]' rows='1' cols='2'>" . $row["correctAnswer"] . "</textarea></span><br />";
        $ret .= "<span class='del'><h6>Delete? <input type='checkbox' name='delete[]'></h6></span><br />";
    $ret .= "</div><br />";

}

答案 2 :(得分:0)

在尝试在查询或字符串中使用php变量时,使用字符串和变量连接方法。 'ORDERBY'后不需要引号。也要小心..这种变化可能对你有帮助。

更改查询:

  

$ query2 =“SELECT * FROM ST_Answers WHERE referencingQuestionID =   '“。$ permIDNum。”'ORDER BY permanentAnsNumber“;

答案 3 :(得分:0)

正如Peterm所说,你不能在'ORDER BY'说明符的代码中引用。一旦修复了这段代码以及更高级别的父代码,一切都会正确显示。