注意:未定义的偏移量:while循环中为0

时间:2014-11-24 22:54:39

标签: php arrays mysqli while-loop offset

我试图将关注者(在关注者表中)中的所有post_id放到一个数组中
这可能是一个很长的代码,但在这里:

while (post_count($con, $session_user_id) > $n) {
$pfields = array("post_id", "from", "content", "good", "favorite"/*, "time", "date"*/);
$query = mysqli_query($con, "SELECT followed_id FROM followers WHERE user_id = $session_user_id");
                while($row = mysqli_fetch_assoc($query)) {
                $data = array();
        $data = $row;
    }
    $t = 0;
    $postids = array();
    if (empty($data) !== true && isset($data)) {
        while ($data[$t] !== NULL) { //ERROR HERE
            $sql1 = mysqli_query($con, "SELECT post_id FROM posts WHERE" . $data[$t] . "= from ORDER BY  posts.post_id DESC LIMIT $t, 100") or die(mysqli_error($con));
            while ($row = mysqli_fetch_assoc($sql1)) {
            $postids = array();
            $postids[] = $row[0];
            }
            $t++;
        if ($t>10) {
            break;
        }
        }
    }
    $sql2 = mysqli_query($con, "SELECT post_id FROM posts WHERE from = $session_user_id ORDER BY  posts.post_id DESC LIMIT $n, 100") or die(mysqli_error($con));
    if ($sql2 === false) {
        echo "An error ocurred";
    } else {
    while($row = mysqli_fetch_assoc($sql2)) {
        $postids[] = $row[0]; //ERROR HERE TOO
    }
    }
        rsort($postids); `


显示的错误是注意:未定义的偏移量:第55行/home/cabox/workspace/LOL/home.php中的0注意:未定义的偏移量:0 / home / cabox / workspace / LOL / home第72行的.php
我已经检查了其他问题而他们没有回答我自己的问题。我检查了查询,他们确实在我的数据库中显示了一些内容。谢谢!

1 个答案:

答案 0 :(得分:1)

使用mysqli_fetch_assoc()时,结果将被格式化为关联数组。所以你需要改变你的代码,如下所示:

while($row = mysqli_fetch_assoc($sql2)) {
    $postids[] = $row['post_id'];
}

正如PHP的文件所说:

  

mysqli_fetch_assoc:将结果行作为关联数组获取

否则,您可能需要使用mysqli_fetch_row()