简体   繁体   中英

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. I am working with MSSQL Server and PHP with PDO because of its safety. I know that fetchColumn() is not the right statement to use, but that's where I gave up because I tried almost everything else.

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.

You could've fixed that by using rowCount() instead of 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(*) . It will return you one row with exactly one column, the value of which you latter get with 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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