Valid PDO Placeholder Characters
Working with placeholders in PHP with PDO can be tricky when it comes to selecting valid characters. To delve into the details, let's first examine the example code provided:
$calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour'); $sth->bindParam(':calories', $calories, PDO::PARAM_INT); $sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12); $sth->execute();
In this example, we're using the placeholder ":colour." What characters are considered valid for placeholders? The answer lies in the source code. Upon inspection, we discover the following regular expression:
BINDCHR = [:][a-zA-Z0-9_]+;
This means that a placeholder must consist of the following elements:
Therefore, valid placeholder characters include:
Hyphens (-), as mentioned in the question, are not valid characters. Alphanumeric characters with underscores provide a convenient and comprehensive range of options for placeholder names.
The above is the detailed content of What Characters Are Valid for PDO Placeholders in PHP?. For more information, please follow other related articles on the PHP Chinese website!