FETCH PDO used incorrectly?

This code fails to correctly grab my record that is already confirmed (a value of 1 in the confirmed column of my database = confirmed. 0 is not. This user I’m grabbing is indeed confirmed. The return it is returning in this function is the return array(true,“”);

public function activate($activationKey)
  {
    $findSub=$this->pdo->prepare("SELECT * FROM Subscribers WHERE activationKey=:activationKey");
    $findSub->execute(array(":activationKey" => $activationKey));
    
    $subDetails=$findSub->fetch(PDO::FETCH_ASSOC);
    
    if($findSub->rowCount()>0)
    {
      if($subDetails["confirmed"]===1)
      {
        return array(false,"alreadyactivated");
      }
      else
      {
        $activateSub=$this->pdo->prepare("UPDATE Subscribers SET confirmed=1 WHERE activationKey=:activationKey");
        $activateSub->execute(array(":activationKey" => $activationKey));
        return array(true,"");
      }
    }
    else
      return array(false,"notfound");
  }

Can anyone spot the issue? It’s something to do with this line here I think.

f($subDetails["confirmed"]===1)

=== is strict datatype comparison. Are you sure $subDetails[“confirmed”] is an Int?

The type in my database is “tinyint(1)”. So…you tell me (not being rude, genuinely don’t know if this counts as being truly an integer.)

Changed to == and it worked… buries head in sand

I’d like to know what type to make my row so I can keep === in instead of == please.

Seems to me it should be.
Maybe PDO iss changing it to a String somehow?

If you can’t do a var_dump() I guess you could risk casting it to an Int, but it would be much wiser to find the root of the problem and fix it.

Loose comparison == is not the best idea

Nah I tried doing “” before and no go.

Agreed.

Currently trying every column type to see which one reads it prperly.

Calling in @r937 for his infinite wisdom.

Hmmm, if not a String, boolean TRUE? That would be bizarre. I don’t see where the code might be doing that.

Ok I’m not going crazy, but boolean didn’t work, so I retried string, and it’s definitely being pulled as a string. The column is set as a TINYINT(4) right now. This is weird.

http://stackoverflow.com/questions/5014522/how-to-get-an-integer-from-mysql-as-integer-in-php
Thoughts?

I suppose you could do
if((int)$subDetails["confirmed"]===1)

but that seems a bit risky and feels dirty.

I would be interested to find out what’s happening too.

I now numeric $_POST vars are sent as Strings, but I’ve never heard of a database doing it.

PHP can cast datatypes (it is “loosely” typed) but I don’t see where that might be happening in your code

News to me, I guess I must have learned to stop using loose comparisons around the same time I started using bind so I never noticed a problem.

I’ll wait for Rudy. I don’t want to hack this. Here is where I submit the subscribers. That 0 on the end is the confirmed column.

$insertSub=$this->pdo->prepare("INSERT INTO Subscribers (email, joinDate, activationKey, confirmed) VALUES (:email, :joinDate, :validToken, 0)");

Hmm…

Yes, some of my earlier code is most horrid. And I suspect if I look back at what I’m writing now I’ll think the same.

I wish I could make a decision as to what to do. I feel like casting is the right option, but I don’t feel either of us actually know the best course of action here :frowning: .

For consistency, I want to keep === and not == but that requires casting, I feel.

IMHO the best way would be to bind the column
http://php.net/manual/en/pdostatement.bindcolumn.php

using the PDO::PARAM_INT CONSTANT if you can
http://php.net/manual/en/pdo.constants.php

Not sure how to implement that with my setup TBH. In all examples I could find, they have a placeholder :example where they set the bind param on the :example placeholder in PDO. I don’t do that.

Put

var_dump($subDetails["confirmed"]);

above your IF statement. I need to see what is contained in that field/value.

$stmt->bindValue(':numMonths', intval($months), PDO::PARAM_INT);

When binding an integer, add PDO::PARAM_INT as in the example above, it makes PDO treat the value being bound as an integer

can’t help you, sorry… i don’t do php

string(1) “0” on the first load (correctly being 0.)
string(1) “1” on the second load (correctly being 1.)

Both strings though.

Can you at least confirm whether MySQL always returns results in varchar instead of the data type of the column (e.g. integer?)

Again, I’m not using a PDO placeholder like in that example. I don’t have hte equivalent :numMonths. I pull the result via a select that bases it off the activationKey. And I just happen to pull another column from that result. I am new to PDO so if that example is still transferable to my situation, I don’t see it.