Php PDO查询未返回预期结果

Php PDO查询未返回预期结果,php,mysql,Php,Mysql,我正在为我的游戏制作一个论坛。目前我正在使用编辑功能。我想弄明白的是,如何检查登录帐户是否拥有发布消息的玩家。你用你的球员发帖,而不是帐号。你可以有几个玩家。一个用户可以分配几个“玩家”。如果他们不拥有发布消息的玩家,我想返回false,但如果他们拥有,则返回true # table accounts id | username | password # table posts id | posterid (this is the player id) | message # table p

我正在为我的游戏制作一个论坛。目前我正在使用编辑功能。我想弄明白的是,如何检查登录帐户是否拥有发布消息的玩家。你用你的球员发帖,而不是帐号。你可以有几个玩家。一个用户可以分配几个“玩家”。如果他们不拥有发布消息的玩家,我想返回false,但如果他们拥有,则返回true

# table accounts
id | username | password

# table posts
id | posterid (this is the player id) | message

# table players
id | account_id | name
这就是我已经走了多远。但不管怎样,这都会返回false。有一个ID为666的帖子,发布该帖子的玩家的账号为34767。所以它应该会起作用

function can_we_edit_post($pid) {

global $db;

// Let's see if they have permission to edit
$stmt = $db->prepare("SELECT * FROM players pl a JOIN posts p ON p.id = $pid WHERE pl.account_id = 34767");
$stmt->execute(array(34767));
$row = $stmt->fetch();

// Check if we got any rows
if ($row) {
    return true;
} else {
   return false;
}

}

if (can_we_edit_post(666)) {
   echo "You may edit this post.";
} else {
  echo "You do not own this post.";
}

您使用的PDO是错误的。你应该这样做:

$stmt = $db->prepare("SELECT * FROM players pl a JOIN posts p ON p.id = :pid WHERE pl.account_id = :id");
$stmt->execute(array(':pid' => $pid, ':id' => 34767));
return (($stmt->rowCount() > 0)? true : false);
PDO和execute是genius,因为您只需提供要用值替换的键,它将转义并使其对您无害


如果查询的其余部分正确,则此代码应该可以工作
rowCount
返回查询返回的行数。如果您只想查看它是否返回任何内容,您可以使用它而不是使用
fetch

您在
pl
之后有一个错误的
a
,因此您的查询可能失败

SELECT * FROM players pl a JOIN posts p ON p.id = $pid WHERE pl.account_id = 34767
                         ^
尝试类似的方法(使用占位符防止SQL注入)-

看看这个SQLFIDLE——没有
a
查询将返回一个结果,如果
a
查询失败并出现错误

编辑

它可能会为每个玩家返回
true
,因为您硬编码了
pl.account\u id
-

WHERE pl.account_id = 34767
并且您没有验证
posterid
是否与特定
post.id
pl.id
匹配,您可以通过将-
和p.posterid=pl.id
添加到您的
JOIN

function can_we_edit_post($pid,$aid) {

global $db;

// Let's see if they have permission to edit
$stmt = $db->prepare("SELECT * FROM players pl JOIN posts p ON p.id = ? AND p.posterid = pl.id WHERE pl.account_id = ?");
$stmt->execute(array($pid, $aid));

// Check if we got any rows
if ($stmt->rowCount()) {
    return true;
} else {
   return false;
}

}


if (can_we_edit_post(666,34767)) { // change 34767 to each player account_id ie. $player->account_id
   echo "You may edit this post.";
} else {
  echo "You do not own this post.";
}

您的查询不应该是
pl.account\u id=?
?是的,我的错!但是仍然不起作用,它总是返回false hmmYou仍然对SQL注入开放;)谢谢我最近刚开始使用pdo而不是mysql和mysqliyears@Pier-卢根德罗:什么?是吗?我以为准备和执行是为了帮我解决这个问题?我知道这是离题的,但你介意进一步解释吗?@Kaka:然后试着用phpmyadmin或其他什么东西运行你的查询,以检查它是否正确。这个脚本应该可以修复它。如果您的查询有问题,您应该在另一个问题中询问。@Sean啊,我忽略了那个变量。固定在我的答案。谢谢,谢谢,我怎么会错过呢。现在我只需要弄清楚为什么它返回true,即使account不拥有该playerIt,它也可能返回
true
,因为您在
pl.account\u id
中硬编码了。我将添加一个带有修复方法的编辑。不管posterid是什么,只要帐户存在,任何人都可以编辑post hmmTry添加
和p.posterid=pl.account\u id
在你的
加入
-
从玩家中选择*加入p ON p.id=?p.posterid=pl.account\u id,其中pl.account\u id=?
。这样,只有当posterid与特定post id的帐户id匹配时,它才会加入。我已将此添加到我的编辑代码示例中。@Kaka:如果您在调用过程中设置了一些错误处理,这个问题就会很清楚。对于数据库,每次调用都应该像预期失败一样进行。
function can_we_edit_post($pid,$aid) {

global $db;

// Let's see if they have permission to edit
$stmt = $db->prepare("SELECT * FROM players pl JOIN posts p ON p.id = ? AND p.posterid = pl.id WHERE pl.account_id = ?");
$stmt->execute(array($pid, $aid));

// Check if we got any rows
if ($stmt->rowCount()) {
    return true;
} else {
   return false;
}

}


if (can_we_edit_post(666,34767)) { // change 34767 to each player account_id ie. $player->account_id
   echo "You may edit this post.";
} else {
  echo "You do not own this post.";
}