gpt4 book ai didi

php - 查询未按顺序返回

转载 作者:行者123 更新时间:2023-11-29 02:00:05 24 4
gpt4 key购买 nike

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

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

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

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

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

    $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++;
}

有人知道从哪里开始吗?我完全被难住了。

最佳答案

您的问题是您在列名称周围使用了引号。以这种方式将常量文字字符串引入 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 statementsPDOMySQLi . Mysql_* 扩展已弃用。

关于php - 查询未按顺序返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17164872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com