简体   繁体   English

PDOStatement PHP MSSQL选择是否结果?

[英]PDOStatement PHP MSSQL Select if result?

I an having a tough time with this and trying to either return 1 or true if my select statement finds a record. 我对此很艰难,如果我的select语句找到一条记录,尝试返回1或true。 I am working with MSSQL Server and PHP with PDO because of its safety. 由于其安全性,我正在使用MSSQL Server和带有PDO的PHP。 I know that fetchColumn() is not the right statement to use, but that's where I gave up because I tried almost everything else. 我知道fetchColumn()不是使用正确的语句,但是那是我放弃的地方,因为我尝试了几乎所有其他方法。

Here is my code. 这是我的代码。

public function activate($email, $email_code) {
        $query = $this->db->prepare("SELECT * FROM users WHERE (email = ? AND email_code = ? AND confirmed = ?)");

        $query->bindValue(1, $email);
        $query->bindValue(2, $email_code);
        $query->bindValue(3, 0);

        try{

            $query->execute();
            $rows = $query->fetchColumn();// HERE I AM NOT SURE WHAT TO USE ??? HELP!
            if($rows == 1){


                $query_2 = $this->db->prepare("UPDATE users SET confirmed =? WHERE email = ?");
                $query_2->bindValue(1, 1);
                $query_2->bindValue(2, $email);             

                $query_2->execute();
                return true;

            }else{
                return false;
            }

        } 
            catch(PDOException $e){
            die($e->getMessage());
        }

    }

The problem with your current code (apart from using quotes around parameter placeholders) is that fetchColumn() gets you the value of the first column in the resultset which is probably some sort of id and that value is not equall to 1. That's why you're always getting false. 当前代码的问题(除了在参数占位符周围使用引号之外)是fetchColumn()您提供结果集中第一列的值,该值可能是某种id,并且该值不等于1。这就是为什么总是假的。

You could've fixed that by using rowCount() instead of fetchColumn() 您可以通过使用rowCount()而不是fetchColumn()

$rows = $query->rowCount();

Now, to check whether a row(s) exist(s) you don't need to actually retrieve the resultset with all columns. 现在,要检查是否存在一行,您实际上不需要检索所有列的结果集。 Just use COUNT(*) . 只需使用COUNT(*) It will return you one row with exactly one column, the value of which you latter get with fetchColumn() 它将返回仅包含一列的一行,您fetchColumn()通过fetchColumn()获得该列的值

Try to change 尝试改变

$query = $this->db->prepare("SELECT * FROM users WHERE (email = ? AND email_code = ? AND confirmed = ?)");

to

$query = $this->db->prepare("SELECT COUNT(*) FROM users WHERE (email = ? AND email_code = ? AND confirmed = ?)");

and

if($rows == 1){

to (just to be safe if for some reason there are duplicates) (为了安全起见,如果出于某些原因重复)

if($rows > 0) {

UPDATE : 更新

Since rowCount() returns number of rows affected by an UPDATE more succinct version of your function might look like this 由于rowCount()返回受UPDATE影响的行数,因此函数的简洁性可能如下所示

public function activate($email, $email_code) {
    $sql = 'UPDATE users 
              SET confirmed = 1 
            WHERE email = ?
              AND email_code = ?
              AND confirmed = 0';

    $rows = 0;

    try{
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $email);
        $query->bindValue(2, $email_code);

        $query->execute();
        $rows = $query->rowCount();
        $query = null;
    } catch(PDOException $e) {
        die($e->getMessage());
    }
    return ($rows > 0);
}

Note : you don't need to bind static values like 0 and 1 for confirmed column. 注意 :对于confirmed列,您不需要绑定诸如01类的静态值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM