0

我有点麻烦...我正在执行一个可能有 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 ... :)

4

4 回答 4

0

如果您有超过 1 个的结果,请使用数组。

$sqlCNextQuestion[] = ...

代替

$sqlCNextQuestion = ...

最后您可以通过 print_r 或 var_dump 函数查看结果

print_r($sqlCNextQuestion);

$sqlBAnswer 等也一样。

于 2013-01-28T10:25:48.407 回答
0

例如,尝试使用 implode 函数

<form action="" method="post">
<input type="submit" name="foo" value="A" />
<input type="submit" name="foo" value="B" />
<input type="submit" name="foo" value="C" />
<input type="submit" name="foo" value="D" />
</form>

<?php
echo implode('', $_POST['foo']);
?>

每个提交按钮中的属性名称: name="foo[]" 然后,在 php 中,$_POST['foo'] 包含一个数组,其中每个 "foo[]" 的值

echo implode('', $_POST['foo']);
于 2013-01-28T10:27:24.753 回答
0

如果您不想使用数组(我不知道您为什么不这样做),您可以将结果连接到一个带有分隔符的字符串中(一个绝对不会出现在任何潜在查询中的分隔符)结果),然后explode()该字符串稍后。

然而,这仍然使它成为一个数组,但另一种方法是通过从中生成子字符串来搜索连接的字符串,所有这些都是完全混乱的。请使用数组:)

于 2013-01-28T10:23:14.280 回答
0

您可以使用PHP 数组来获得最佳输出。

//Count the number of rows output
$sqlALastQuestionAnswered = array();

$sqlACount = mysql_num_rows($sqlA);
$count = 0;
//If rows exist, define the values
if ($sqlACount > 0) {
    while ($row = mysql_fetch_array($sqlA)) {
        $sqlAPKID[$count] = $row["PKID"];
        $sqlALastQuestionAnswered[$count] = $row["LastQuestionAnswered"];
        $count++;
    }
}

计数变量将包含结果的总数。

现在您将能够通过数组索引访问特定元素,例如 $sqlALastQuestionAnswered[0]将返回第一个值,依此类推。

于 2013-01-28T10:34:43.243 回答