How the PDO::PARAM_STR parameter works in PHP+PDO

Asked

Viewed 1,120 times

2

Example:

$sth->bindValue(':calories', $calories, PDO::PARAM_INT);

PDO::PARAM_INT This parameter is a kind of validation for data type ? If it is, why is there no error when I pass an integer?

  • I edited your code. I ask that whenever you post a question you try to identify the code (leave 1 blank line after the text, and then add 4 spaces before the code "so that it is interpreted as code", or just select the code and then click on the editor { }. Other than that: you mentioned PARAM_STR, but in his question was PARAM_INT. Which one is right?

  • 1

    It is not a validation. PDO uses this parameter to identify how to mount the query correctly. For example, strings should be in quotes, no longer numbers.

  • 1

    I haven’t seen an official explanation for this yet, in practice it’s exactly as @Oeslei said. I only saw the difference to use the clásula limit

1 answer

1


Just like the @Oeslei said, PDO::PARAM_INT (or STR), does not validate the value type.

To do this in case you must use the filter_var, for example:

if (filter_var($suaVariavel, FILTER_VALIDATE_INT)) {
    // válido
} else {
    // inválido
}

Useful links

PHP Filter: http://php.net/manual/en/book.filter.php


Any questions, leave a comment below.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.