【问题标题】:Notice: Undefined Index inside 'while loop'注意:'while 循环'中的未定义索引
【发布时间】:2013-09-09 21:47:33
【问题描述】:

出现这些错误的原因是什么?

Notice: Undefined index: photo
Notice: Undefined index: username

它们出现在 while 循环中,在 '.$row['photo'].' 上。和 '.$row['username'].'

我只是如下调用PHP页面上的公共函数。

<?php echo $userSuggestions($uiD); ?>

我在 while 循环中需要它的原因是我正在尝试使用 PDO::FETCH_ASSOC,因此它不会在回显时重复相同的结果。

我可以使它与 $row = $sth-&gt;fetch() 一起工作,然后在 php 页面上调用它如下:$userSuggestions['photo'], $userSuggestions['Username'] 但这样我将无法循环它并检索不同的结果(不同的信息而不是一遍又一遍地重复同一个)。

我已经搜索了各种线程以得出需要 isset() 的结论,但无法理解为什么在此查询中需要 isset,如果有人能够解释我将不胜感激。 谢谢。

public function userSuggestions($uiD)
{
    $sth = $this->db->prepare("
                                SELECT F.friend_two AS possible_friend, U.username, U.uiD, U.photo
                                FROM user_friends F, users U
                                WHERE F.friend_one IN (SELECT friend_two FROM user_friends WHERE friend_one = :uiD) 
                                AND F.friend_two NOT IN (SELECT friend_two FROM user_friends WHERE friend_one = :uiD)
                                AND NOT F.friend_two = :uiD
                                AND U.uiD NOT IN (SELECT friend_two FROM user_friends WHERE friend_one = :uiD) 
                                AND U.photo NOT IN (SELECT friend_two FROM user_friends WHERE friend_one = :uiD) 
                                GROUP BY possible_friend
                                ORDER BY RAND()
                                ");
    $sth->execute(array(':uiD' => $uiD));

    while($row = $sth->fetchAll(PDO::FETCH_ASSOC))
    {
        echo '
                <div class="SuggestionsPhotoDiv">
                    <img src="'.$row['photo'].'" class="SuggestionsPhoto">

                    <span class="SuggestionsButton">
                        <a>Follow Me</a>
                    </span>

                    <span class="SuggestionsName">
                        <a>'.$row['username'].'</a>
                    </span>
                </div>
        ';      return $row;

    }

【问题讨论】:

  • 这意味着在它迭代的行上,这些项目在 $row 数组中不存在。在 while 循环的开头尝试 var_dump($row) 进行故障排除。
  • 这很有趣,如果我这样做的话 var_dump($row);在循环中,我返回了照片和用户名数组,但是当我尝试打印它时,它给了我错误。
  • @iBrazilian - 您能否发布var_dump() 的摘录,以便我们查看返回的内容?
  • @Joe 我已经看到您的回答,感谢您的回复。在 while 循环中,它返回用户名和照片,但是当我打印它时,它不起作用并给出未定义的索引。数组(2) { [0]=> 数组(4) { ["possible_friend"]=> string(3) "154" ["username"]=> string(5) "Emily" ["uiD"]=> string(3) "151" ["photo"]=> string(96) "uploads/profilePictures/image2.jpg" } [1]=> array(4) { ["possible_friend"]=> string(3) " 153" ["username"]=> string(5) "Emily" ["uiD"]=> string(3) "151" ["photo"]=> string(96) "uploads/profilePictures/image1.jpg" } }
  • 我尝试了您的两个解决方案,它们都是正确的,谢谢您的解决方案。

标签: php mysql sql pdo


【解决方案1】:

解决方案 1:

while($row = $sth->fetch(PDO::FETCH_ASSOC)){
    // your current code here
}

这使用fetch 而不是fetchAllfetchAll 返回一个包含所有结果的数组,这意味着当您尝试访问$row['username'] 时,它不存在。

解决方案 2:

$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
    // your current code here
}

这将输出作为数组数组,然后循环遍历它们。

fetchAll 函数文档:http://php.net/manual/en/pdostatement.fetchall.php

这是您的fetchAll 输出:

array(2) {
    [0]=> array(4) {
        ["possible_friend"]=> string(3)"154"
        ["username"]=> string(5) "Emily"
        ["uiD"]=> string(3) "151"
        ["photo"]=> string(96) "uploads/profilePictures/image2.jpg"
    }
    [1]=> array(4) {
        ["possible_friend"]=> string(3) "153"
        ["username"]=> string(5) "Emily"
        ["uiD"]=> string(3) "151"
        ["photo"]=> string(96) "uploads/profilePictures/image1.jpg"
    }
}

您可以看到它正在输出所有结果,因此顶级数组中不存在 username 键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2015-12-01
    相关资源
    最近更新 更多