pdo - 用户名已经存在,但还不是

发布于 2024-12-17 08:26:24 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笑忘罢 2024-12-24 08:26:24

select count(*)...始终返回一条记录,即使没有任何匹配

,因此,您可以尝试以下操作:

select 1 from ...;

Between,检查任何记录返回的正确方法应该使用 rowCount

如其余部分所示:-

  1. 您应该使用绑定而不是使用引用
  2. 函数应该始终返回一个值

select count(*)... will ALWAYS return a record, even nothing is matched

so, you can try this :

select 1 from ...;

between, a proper method to check any record return should be using rowCount

as indicate by the rest :-

  1. you should make use of bind rather than using quote
  2. function should always return a value
秉烛思 2024-12-24 08:26:24

当查询失败时,您的 isUsernameAvailable 函数将返回 NULL(等于 FALSE)。这将触发 用户名已被占用! 消息。

看起来你的 SQL 查询总是失败,我认为这并不奇怪,因为你盲目地在它上面应用引号。

运行SQL查询后,您必须更妥善地处理查询失败的情况,例如显示发生了内部SQL错误并记录下来,以便您可以查看日志并定位错误原因。

此外,您应该更正确地使用 PDO,特别是当您想要防止 SQL 注入时(您必须关心这一点)。请参阅在 PHP 中停止 SQL 注入的最佳方法

Your isUsernameAvailable function will return NULL (equals to FALSE) when the query fails. This will trigger the The username is already taken! message.

Looks like your SQL query always fails, which I think is no wonder because you blindly apply quotes onto it.

After running a SQL query, you must more properly deal with the case that the query failed, e.g. display that an internal SQL error occured and log it, so you can look into the log and locate the cause of the error.

Additionally, you should use the PDO more properly, specifically when you want to prevent SQL injection (which you must care about). See Best way to stop SQL Injection in PHP.

渡你暖光 2024-12-24 08:26:24

使用 rowCount() 而不是 fetchColumn()

  if ($sth->rowCount() > 0) {

Use rowCount() instead of fetchColumn()

  if ($sth->rowCount() > 0) {
魔法唧唧 2024-12-24 08:26:24

如果没有列,则 fetchColumn 返回 false,另请参阅 ajreal 答案。

    if ($sth = $this->db->query($sql)) {
        if ($sth->fetchColumn() == false) {
            return true;
        } else {
            return false;
        }
    }

fetchColumn returns false if there is no column and also see ajreal answer.

    if ($sth = $this->db->query($sql)) {
        if ($sth->fetchColumn() == false) {
            return true;
        } else {
            return false;
        }
    }
北笙凉宸 2024-12-24 08:26:24

您可以尝试使用以下代码吗?

$st->rowCount() > 0 

Can you try using the following code?

$st->rowCount() > 0 
半城柳色半声笛 2024-12-24 08:26:24
$sql = "SELECT COUNT(*) FROM users WHERE username = '$username'";
$sql = $this->db->quote($sql);

您正在引用整个查询,这没有任何意义,并且从安全角度来看是完全危险的 - 并且可能会导致无效的查询。

这是更正确的代码:

// return TRUE if user is available
public function isUsernameAvailable($username) {
    $sql = "SELECT id FROM users WHERE username = :username";
    $stmt = $this->db->prepare($sql);
    $stmt->execute(array(':username' => $username));

    return $stmt->fetch() === FALSE;
}


if (!$user->isUsernameAvailable($_POST['username'])) {
    echo 'The username is already taken!';
    $error = 1;
}
$sql = "SELECT COUNT(*) FROM users WHERE username = '$username'";
$sql = $this->db->quote($sql);

You are quoting the whole query, this does not make any sense and is outright dangerous from a security standpoint – and probably results in an invalid query anyway.

Here's a more proper code:

// return TRUE if user is available
public function isUsernameAvailable($username) {
    $sql = "SELECT id FROM users WHERE username = :username";
    $stmt = $this->db->prepare($sql);
    $stmt->execute(array(':username' => $username));

    return $stmt->fetch() === FALSE;
}


if (!$user->isUsernameAvailable($_POST['username'])) {
    echo 'The username is already taken!';
    $error = 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文