我有点麻烦...我正在执行一个可能有 5 个结果(1、2、3、4 或 5)的查询,并将它们放入 PHP 变量中。我需要立即在另一个查询中使用这个变量,在这个表中只有这 5 个值中的 1 个存在。
目前,我认为该变量仅持有“5”,即找到的最后一个结果,并且如果当前值不是“5”,则随后在第二个查询中找不到任何结果。
我很想知道是否有一种方法可以将所有 5 个值存储在一个变量中,并在第二个查询中循环遍历它们而不使用数组?
我的代码在这里:
//Find the User's ID and the ID of the last question answered
$sqlA = mysql_query("SELECT PKID, LastQuestionAnswered FROM User WHERE EmailAddress = '***'");
//If the operation produces an error, output an error message
if (!$sqlA) {
die('Invalid query for SQLA: ' . mysql_error());
}
//Count the number of rows output
$sqlACount = mysql_num_rows($sqlA);
//If rows exist, define the values
if ($sqlACount > 0) {
while ($row = mysql_fetch_array($sqlA)) {
$sqlAPKID = $row["PKID"];
$sqlALastQuestionAnswered = $row["LastQuestionAnswered"];
}
}
//Find the answer given by the user to the last answered question
$sqlB = mysql_query("SELECT Answer FROM Responses WHERE User = $sqlAPKID");
//If the operation produces an error, output an error message
if (!$sqlB) {
die('Invalid query for SQLB: ' . mysql_error());
}
//Count the number of rows output
$sqlBCount = mysql_num_rows($sqlB);
//If rows exist, define the values
if ($sqlBCount > 0) {
while ($row = mysql_fetch_array($sqlB)) {
$sqlBAnswer = $row["Answer"];
}
}
//Find the number of the next question to be answered based on the user's previous answer and the question they answered
$sqlC = mysql_query("SELECT NextQuestion FROM Answers WHERE QuestionNumber = $sqlALastQuestionAnswered AND PKID = $sqlBAnswer");
//If the operation produces an error, output an error message
if (!$sqlC) {
die('Invalid query for SQLC: ' . mysql_error());
}
//Count the number of rows output
$sqlCCount = mysql_num_rows($sqlC);
//If rows exist, define the values
if ($sqlCCount > 0) {
while ($row = mysql_fetch_array($sqlC)) {
$sqlCNextQuestion = $row["NextQuestion"];
}
}
//If there is no value for $sqlCNextQuestion, the user has finished the survey
if (empty($sqlCNextQuestion)) {
//Redirect the user to the "Survey Complete" page
echo '<meta http-equiv="refresh" content="0; URL=surveycomplete.php">';
//If there is a value for $sqlCNextQuestion, the user has not finished the survey
} else {
等等
因此,目前,如果没有找到 的值$SQLCNextQuestion
,用户将被重定向到“调查完成”页面。
任何想法和帮助将不胜感激,请放心,在推出之前将编辑代码以利用 PDO ... :)