我创建了一个查询来检查“朋友关系”。基本上,friend_one
/$user_id
是用户登录和friend_two
/$profile_user
是其他用户的ID ... ie:如果我点击另一个用户的配置文件在这里,它是该用户。在SELECT查询上进行扩展
我的系统工作正常,除了当登录用户查看他们自己的配置文件。由于我的查询正在检查登录的用户查看其他配置文件,因此当登录用户查看他们自己的配置文件时,它会抛出未定义的变量错误。
有什么我可以做我的SQL
或php
,我可以改变,所以当friend_one
/user_id
看法有自己的空间,它不抛出无法识别变量错误?
第一个无法识别的变量错误从这里开始:if ($friend_status == 2) {
。所以查询不会引发错误。
错误显示,因为我永远不会有一个用户在这个表中与他们自己的关系。所以,我猜这个错误也会在查看“不满意”的配置文件时发生。任何想法的方法来解决它?
编辑:
我原来的答复有点难以理解。基本上唯一的一次朋友“关系”或插入到我的数据库是如果一个用户朋友的另一个用户。所以,如果这个关系不在数据库和我的查询中,我想“放开它”,所以它不仅检查friend_one
和friend_two
的关系。
$okay = true;
//Checks
if ($okay) {
$friend_sql = "
SELECT *
FROM friends
WHERE friend_one = ?
AND friend_two = ?
";
$select_friend_stmt = $con->prepare($friend_sql);
$select_friend_stmt->execute(array($user_id, $profile_user));
$friend_rows = $select_friend_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($friend_rows as $friend_row) {
$select_friend_1 = $friend_row['friend_one'];
$select_friend_2 = $friend_row['friend_two'];
$friend_status = $friend_row['status'];
$friend_status_date = $friend_row['date'];
}
}
else {
echo "Friend Status not found.";
}
$friend_status_button = null;
if ($friend_status == 2) {
$friend_status_button = "Approved";
}
else if ($friend_status == 1) {
$friend_status_button = "Pending";
}
else if ($select_friend_1 == $select_friend_2) {
$friend_status_button = null;
}
else {
$friend_status_button = "Add Friend";
}
为什么不检查'$ user_id == $ profile_user',然后绕过这个检查自己是否是'朋友'的整个代码。 – Sean
@Sean好主意,但是当我写出这个问题时,我意识到任何非关系用户仍然会为我的查询抛出一个错误。所以,我认为这是更多情况来检查'$ profile_user'是否不在数据库中以允许查询运行....但我仍然需要检查'$ user_id == $ profile_user',就像你一样指出。这是我的查询和我刚刚指出的两件事的结合......只是无法弄清楚如何编写它。 – Paul
在php中,你可以设置'$ friend_status'的默认值(即''friend_status = 0;'),只有在返回一行时才会改变。这样就不会在'if($ friend_status == ...'错误,或者如果你想总是从MySQL返回一行,你可以使用默认行值的'UNION' - 参见http:// stackoverflow .com/a/3417312/689579 – Sean