public function getfriends($username){
// gets a users friends by username... does cross checking and joins the users table to it so we can get other data without having to do another query
$sql = "SELECT S.ts, S.stats, S.image, PI.rusername, PI.arefriend,
PI.tusername AS friend
FROM friends PI
JOIN users S ON PI.tusername = S.username
WHERE S.username = PI.tusername
AND PI.tusername!=:username
AND PI.rusername=:username
AND S.username!=:username
UNION
SELECT S.ts, S.stats, S.image, PI.tusername, PI.arefriend,
PI.rusername AS friend
FROM friends PI
JOIN users S ON PI.rusername = S.username
WHERE S.username = PI.rusername
AND PI.rusername!=:username
AND PI.tusername=:username";
$stmt = db::prepare($sql);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount())
{
// Loop through the assoc array and create a new array we can return
// after processing is done.
// fetch assoc as suggested by Jon removes the duplicates
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$xrow[]=$row;
}
return $xrow;
} elseif(!$stmt->rowCount()) {
return 0;
}
}
这是我的函数,它是类的一部分,您基本上为它提供了一个用户名,并为该用户选择了所有朋友并将其放入数组中。这样,无论我在哪里调用此函数形式,我都可以遍历结果。朋友数据库表中的这两列称为tusername和rusername。这基本上是在跟踪谁向谁发送了好友请求。它是tousername和requestusername的缩写。
现在某个值丢失了,这是返回的数据的打印机
Array
(
[0] => Array
(
[ts] => 2014-01-15 08:27:17
[stats] =>
[image] => uploads/girl.jpg
[rusername] => nasser
[arefriend] => 2
[friend] => girl
)
)
如您所见,rusername存在,但tusername丢失,并且tusername中的数据是错误的,它只是显示传递给getfriends函数的用户名。
非常感谢你的帮助。
最佳答案
我不知道你为什么在这里使用工会。
尝试这个
SELECT S.ts, S.stats, S.image, PI.rusername, PI.arefriend,
PI.tusername AS friend
FROM friends PI
JOIN users S ON PI.tusername = S.username
WHERE S.username!=:username
关于php - 这个MySQL查询有什么问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21139034/