gpt4 book ai didi

php - 这个 MySQL 查询出了什么问题

转载 作者:行者123 更新时间:2023-11-30 00:49:00 26 4
gpt4 key购买 nike

  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;
}
}

这是我的函数,它是类的一部分,您基本上给它一个用户名,它会选择该用户的所有 friend 并将它们放入一个数组中。这样我就可以在调用此函数形式的任何地方循环遍历结果。 friend 数据库表中有两列,称为 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 函数的用户名。

非常感谢您的帮助。

最佳答案

我不知道你为什么在这里使用 union 。

试试这个

   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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com