出现这些错误的原因是什么?
Notice: Undefined index: photo
Notice: Undefined index: username
它们出现在 while 循环中,在 '.$row['photo'].' 上。和 '.$row['username'].'
我只是在 PHP 页面上调用公共函数,如下所示。
<?php echo $userSuggestions($uiD); ?>
我在 while 循环中需要它的原因是我正在尝试使用 PDO::FETCH_ASSOC 因此它在回显时不会重复相同的结果。
我可以让它工作,$row = $sth->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;
}