【问题标题】:Occasionally a record is missing from PHP/JSON arrayPHP/JSON 数组中偶尔会丢失一条记录
【发布时间】:2013-03-18 14:13:57
【问题描述】:

我正在调用 PHP 脚本来获取结果,然后将数组转换为 javascript 数组。我的数组是一个多项选择测验,可以有 2 到 6 个答案,它用答案拉出问题。

我一直在进行大量测试,有时会在预期的情况下丢失记录。例如,在拉下一个应该有 5 个答案的问题后,只拉出 4 个答案。即使我重新加载测验,也会丢失相同的结果。

这似乎是随机的,这个问题出现的时间没有规律,例如,第一次测试发现第一个有六个答案的问题缺少一个答案结果。另一个测试发现第三个问题是一个四个答案的问题,缺少一个答案结果。

我使用了 Javascript 控制台,当其中一个问题缺少结果时,我可以识别的唯一模式是 answerid 始终是第一个,即如果 answerid 是 285、286、287,然后是 285将是缺少的。

在 25 个测试问题中,只有 3 个缺少结果。

这是我的代码,PHP 脚本...

$thisquizid = $_REQUEST['quizidvalue'];
$questionquery = pg_query($db_handle, "SELECT * FROM question LEFT JOIN answer USING (questionid) WHERE quizid = '$thisquizid'");
$questionrows = pg_num_rows($questionquery); 
$questionresult = pg_fetch_array($questionquery); 

$questions = array();

while($row = pg_fetch_array($questionquery)) {
    $questions[$row['questionid']][] = $row;
}
die(json_encode($questions));

还有我的 Javascript

var questions;
var currentquestion = 0;


$(document).ready(function(){

        console.debug("in ajax");
        jQuery.ajax({

                    error: function (data, textStatus, errorThrown){
                        console.log(data);
                        console.log(textStatus);
                        console.log(errorThrown);
                    },
                    url:"quizajax.php",
                    dataType: "json",
                    data: { 
                        quizidvalue: <?=$thisquizid?>
                    },


            }).done(function(data) {
                    questions = data;
                    for(i in data){
                        console.log(data[i]);

                    }
                });


        $("#nextquestionbtn").click(function () {
            nextQuestion();
        });
    });

function nextQuestion (){
    for(i in questions) {
        if(i<=currentquestion)
            continue;

        currentquestion = i;


        for(y in questions[i]) {
            console.log("CurrentA: "+ currentquestion);
            console.log("I: " + i);
            console.log(questions[i][y].answerid);
        }


        console.log("CurrentQ: "+ currentquestion);
        console.log("I: " + i);
        console.log(questions[i]);

        questionVariables ();


        break;
    }

这是一个关于它在我的数据库中的记录的问题

questionid | quizid | questiontype | qdescription | qfilelocation | noofanswers | answertype | answerid | adescription | afilelocation | iscorrect 
------------+--------+--------------+--------------+---------------+-------------+------------+----------+--------------+---------------+-----------
        295 |     57 | text         | 6mark work   | null          |           6 | text       |      795 | sadfds       | null          | t
        295 |     57 | text         | 6mark work   | null          |           6 | text       |      796 | asdfsd       | null          | f
        295 |     57 | text         | 6mark work   | null          |           6 | text       |      797 | asfsadf      | null          | f
        295 |     57 | text         | 6mark work   | null          |           6 | text       |      798 | asdfsadf     | null          | f
        295 |     57 | text         | 6mark work   | null          |           6 | text       |      799 | asdfsadf     | null          | f
        295 |     57 | text         | 6mark work   | null          |           6 | text       |      800 | sadfasdf     | null          | f

【问题讨论】:

    标签: php javascript arrays json multidimensional-array


    【解决方案1】:

    您丢失的行来自 PHP 中的逻辑错误。

    在您的示例 $questionrows = pg_fetch_array($questionquery); 的第 4 行中,您正在调用 pg_num_rows()。这会获取第一行,然后由于 while 循环 while($row = pg_fetch_array($questionquery)) 的测试条件而忽略它,它再次获取数据集的下一行。

    由于在循环之前似乎不需要任何行内容,我建议评论或删除第 4 行。

    【讨论】:

    • php.net/manual/en/function.pg-num-rows.php - 我没有看到有关在 pg_num_rows 上获取数据的信息
    • 更正:他在第 4 行调用 pg_fetch_array,而不是 pg_num_rows。
    • 注释掉了,你是对的,这是不必要的,但是问题仍然存在。
    • 在您的编辑中,它运行良好!非常感谢克里斯托弗
    猜你喜欢
    • 2021-11-22
    • 2020-05-19
    • 2020-05-02
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多