thekabal / tki

The Kabal Invasion - A web based space exploration (4x) game
Other
11 stars 7 forks source link

Should we use PDO datatype Constants in queries using bindParams (and bindValues)? #48

Closed urbndecay closed 7 years ago

urbndecay commented 8 years ago

I am working on the register / login pages and re-writing the code for registration and authentication of players. I checked a few of the updated classes and didn't see PDO constants in use. I have normally used them in the past and wanted to find out if we (I) should or shouldn't use them? If not then no big deal, I just want to keep my code consistent with the code base as it is being updated.

More details

Here is an example

$sql = "SELECT email, character_name, ship_name FROM ::prefix::ships WHERE (`email` = :email) OR (`character_name` = :characterName) OR (`ship_name` = :shipName)";

        $stmt = $pdo_db->prepare($sql);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR);
        $stmt->bindParam(':characterName', $characterName, PDO::PARAM_STR);
        $stmt->bindParam(':shipName', $shipName, PDO::PARAM_STR);

        $stmt->execute();
        $count = $stmt->rowCount();

So instead of


        $stmt->bindParam(':email', $email);

it would be


        $stmt->bindParam(':email', $email, PDO::PARAM_STR);`
thekabal commented 8 years ago

Sure! No objection from me!

thekabal commented 7 years ago

This turned out to be extremely impactful & useful. Thanks to this comment and to PHPStan, I discovered that the score update was converting the variable upon binding from an int to a string (!!). This saved a ton of debugging for me! Thank you VERY much for the suggestion - I'll be performing these changes as I go now!!